• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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 .
0
redhat092798
Asked:
redhat092798
  • 5
  • 3
  • 2
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now