PHP Tips (4) How do I get a line of data in a TXT file?

zhaozj2021-02-16  48

I have read a lot of single text data, I found that these programs are very efficient, every time you have to read the entire file,

Ask a method, you can get the data I want, but only read the data!

Can only use PHP!

-------------------------------------------------- -------------

Use fseek () positioning

Then FGETS

-------------------------------------------------- -------------

Not everyone like so stupid, because! File can only read it! This is the magnetic disk principle! Don't dream! So everyone is so writable, I have been using text to store data! Your question should look at the disk storage principle You will be file, explode is so reading!

-------------------------------------------------- -------------

Maybe

$ Fd = fopen (...);

For ($ I = 0; $ i

$ Str = FGETS ($ FD);

$ Str = CHOP ($ STR);

It can be done better for big files.

-------------------------------------------------- -------------

If you want to save your file size, such as 1K, if you are not enough 1K, use space to fill, then you use fseek () and fgets will be very fast.

FSeek ($ FD, 1000 * n, seek_set);

$ Content = FREAD ($ FD, 1000);

$ Content = RTRIM ($ Content);

-------------------------------------------------- -------------

I think it is, suppose you have a text file for 100,000 records, if the record is probably

100m, you only open the file, then locate the data of 1k size. Time should be very fast, and there is very little memory resources.

If you read the file in an array with a file () method, read the single record, I think, how can you not be better than the previous method.

Moreover, you must have multiple lines in the message, use the file () method you need to perform special processes for the message in saving to the file, and if you fix the record, you only need to add it.

But fixed records have some drawbacks of files that need to be deleted, and they will be wasted in some spaces.

-------------------------------------------------- -------------

Continuously fill fill, save space! How much data can you put in a normal website? If the data is too large, please use the database

-------------------------------------------------- -------------

To Langwan:

Don't misunderstand, I am talking about the use of fixed records will waste space!

If the message is mostly very short, you can decrease 1k to 100B.

In fact, this is that it is only for the use of files to save data, of course, it is best to use the database (more than the record number).

It should be allowed to make a hundred flowers!

-------------------------------------------------- -------------

Oh, don't fight. . . . . ^ _ ^. . . . . . . I think if the file is more content, use the database. Otherwise why is the library?

And the text documents are not ideal. If there are too many files that take advantage of the space that exceeds the file size (related to cluster). 10 1MB files are absolutely small than 100KB files.

Talking about the use of the database.

-------------------------------------------------- -------------

The product database is indeed not high. Unless there is a good data structure, I haven't found a good information, I haven't found a good information)

With FGETS for small files, the efficiency of small files may be not high, but for large files, you will find that you waste most of the time (especially what is needed in front).

These functions are some time I am faster than you, and you are more than I am more than me. You can't kill a stick.

-------------------------------------------------- -------------

The idea is a bit similar to the MEMO field of the DBASE data table, which is of course possible with that method. But to consider the efficiency problem: you will handle index information first, then locate the actual record position to read the corresponding content, maybe it will be lost. Moreover, PHP seems to have a built-in support for the DBASE data table, I think, if you use this method, you will not use a * .dbf data table to store data! What do you think?

-------------------------------------------------- -------------

FSEEK just positions the file read-write pointer, which should be the underlying interface of the operating system processing file and not actually read the data. The FGETS should be read from the current location, which should take a certain disk I / O time, and file () is to read the text file to an array, which should be provided by the programming language. A advanced function, relatively, the occupied processing time In addition to the disk I / O time, there must be the required time required to execute the required handler. In fact, the main bottleneck in the document reading should be a disk access speed (if more subsequent processing procedures are not involved.), The more the content you read, the longer the access time should be, When processing a small file (the access content is not large), what is the problem? However, if the file is relatively large, this time the gap should be more obvious.

-------------------------------------------------- -------------

Under the same condition, the two functions are not necessarily :)

Objective is the principle of disk storage!

Why not necessarily? :) Ohham Wan Word Call!

The same condition is the same condition, but the same condition in any state is not the same!

So you should study a curve, a curve of these functions!

Objective is the existence of such a curve! Unless you encounter text data forever along a straight line!

And objective is the status of data is a curve!

Hehe, I wish you success! If you succeed, tell me!

Because the Way has never used PHP MySQL, my website is less payment! :- (if you solve, the biggest beneficiary Way!

===================

The same condition in any state is not the same!

-------------------------------------------------- ------------- Now is the story of the Wan Wan:

Have a very powerful person, he said: light is wave. Later, there was a very powerful person, he said: Light? Not wave, light is particle. Two thoughts played in the academic world! Einstein said: "It's good, your research is right, and the light has" wave dimension "! The world is finally not arguing!

Way began to yell! FSEEK, FGETS, FILE, text databases have multiple images, haha!

Don't quarrel this, smart people are used! Different places, with different functions!

Text requires a good discussion!

===============================

The product database is indeed not high. Unless there is a good data structure, I haven't found a good information, I haven't found a good information)

===============================

Why can't you find a good information?

===============================

Wave Wan tells the story time:

One day, the classmates of the waves came to me, he asked the waves, your strange strange software, the display speed is not slow! How did you get? Other software make this speed! Algorithm teaches me!

Wan Wan said to him: "Hey, it is easy, there is no algorithm, the most stupid method used! More than 400 contour points are calculated with long hair, put it in an array. The computer is not long, the head is not long. It's not enough! So it's fast! Sometimes the classmates say: What?, Oh yeah, what, every different shape, I have made an array, such a fool, can you do?

================================

Summary: For the script you want to do, design the most direct most objective data structure is the fastest! Unable to general all objective, let all objective use the theory that does not exist!

================================

If you want to wear the most comfortable clothes, you can only find someone to do it, this problem! too. . . . ================================

GM is theoretical

Oh, a hundred flowers, constantly innovation!

转载请注明原文地址:https://www.9cbs.com/read-20188.html

New Post(0)