Read a line and the next and the next...

I want to read a line at a time in a text file. For example I have in my text file:
green
blue
yellow
read
pink

How do I read the first line, then the second line and then the next and so on ?One at a time but automatically next after next .
redhat092798Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
#include <stdio.h>

FILE* fp = fopen ( "myfile.txt", "r");
char acBuf [ MAX_BUF];

while ( fgets ( acBuf, MAX_BUF, fp))
{
 // line is now in 'acBuf', do something with it...
}

fclose ( fp);

Feel free to ask if you need more information!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
Except, i would not use C file streams for new programs.  You can do the same with C++ fstream objects.  These have many advantages over the old file streams.  For example, you don't have to worry about a maximum line length, like

fstream Fil("myfile.txt",ios::in);
string Lin;

while (!Fil.eof())
{
   getline(Fil,Lin);
}
0
jkrCommented:
>>Except, i would not use C file
>>streams for new programs

Err, may I ask why? ;-)
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

nietodCommented:
fstreams have many advantages.  better type safety, the ability to work with strings (as in the code above), code can work with other stream classes, exception safety, the class uses destructors to insure files are closed.   lots more I'm sure.
0
jkrCommented:
0
nietodCommented:
God, you've got some kind of memory....

But I'm not sure what you're point is.  I didn't sugget the use of the old C streams there  

 Unless it is the fact that I say I don't use stream objects in my code, which is true, because I use the windows API.  But that is because I do need the ability to lock records and other tasks not supported by standard C++ and I do not need portability.  If I needed portability and the standard streams supported the features I needed, I would use the C++ stream classes over the C file streams, no question about it.
0
redhat092798Author Commented:
ok, I think you answered 70% complete. I asked  "How do I read the first line, then the second line and then the next
and so on ?One at a time but automatically next after next ."
For example, I copy the first line and I do something to it, then I copy the second line and do something to it and then the third line and so on. All this automatically till the last line in my file .
0
jkrCommented:
That's what both code samples do. You could e.g.

FILE* fp = fopen ( "myfile.txt", "r");
char acBuf [ MAX_BUF];
char acLine [ MAX_LINE];
int i = 0;
while ( fgets ( acBuf, MAX_BUF, fp))
{
 // line is now in 'acBuf', do something with it - e.g. displaying it
 printf ( acLine, "Line %d is %s\n", i, acBuf);
}

The 'while ( fgets())' loop will read line after line until the end of the file is reached or you terminate it using a 'break' statement....

fclose ( fp);
0
jkrCommented:
Ooops, I forgot ;-)

while ( fgets ( acBuf, MAX_BUF, fp))
{
 i++; // <-- sorry ;-)
 // line is now in 'acBuf', do something with it - e.g. displaying it
 printf ( acLine, "Line %d is %s\n", i, acBuf);
}
0
redhat092798Author Commented:
Excellent ! Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.