How to make a fixed length (of offset) for each line in text file
I use PHP to read lines by given offset (using fseek) as
fseek($fp, 51);
$data = fgets($fp, 4096);
The length 开发者_StackOverflow社区of each line is less than 10 characters. Can I make a fixed length of 10 to have the start of each line as 0,10,20,30,40, ....
The lines are as
first
second
third
As a matter of fact, I want to virtually (fake indeed), tell fseek()
that each line is 10 characters.
It sounds like you should use fgets. There's no way to emulate lines being 10 characters when they're not without reading data and then processing it (there's no way to seek without knowing where to seek).
You could just use fgets to look through the lines and then substr() them all to 10 charactes
If the reason you're wanting to do this is for performance reasons, you will probably need to store the data differently (or use indexing). To be able to seek to a specific entry you would need to be able to figure out which etnry to seek to. The easiest way to allow that would be via padding of lines. (If each line entry is 10 characters, then can just seek and trim based on 10*x)
精彩评论