Jump to content

How do I read a text file...2 lines at a time?


vexusdev

Recommended Posts

I have tried numerous different codes and I can't seem to get this working right.

 

I want to read a text file say 100 lines at a time.

 

If you guys need a little bit of clarification.

 

 

say I have a text file with

 

Nick

Bob

John

Larry

Ashley

Britney

Tom

Lisa

Joshua

 

Say I need to process that list 2 lines at a time.

 

First chunk [Nick,Bob]

Second chunk [John,Larry]

Third chunk [Ashley,Britney]

 

Does anyone have the slightest idea of how to accomplish that?

Link to comment
Share on other sites

if u have huge mounts of data why not use databases?

 

Well because the user will be uploading a list and this list will be converted(by querying a database) and then rewriting to another text file.

 

Also the code that you wrote.. won't that only read the first 2 lines of the file? I need to read the whole file 2 lines at a time. I just can't grasp how to accomplish this because of my limited knowledge.

Link to comment
Share on other sites

$fh = fopen('filename','rb');
while(!feof($fh)) {
$line1 = fgets($fh);
if(!feof($fh)) {$line2 = fgets($fh);}
//do something with these two lines
}

 

[edited]

 

Its fgets obviously, not fread

 

Thank you, say I wanted to expand that to read 100 lines at a time is it possible?

 

What if i made a loop from 1 to 100 with fgets inside the while loop and insert the data into an array?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.