开发者

How to make a fixed length (of offset) for each line in text file

开发者 https://www.devze.com 2023-04-11 09:54 出处:网络
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 len

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号