Jump to content
Sign in to follow this  
c0ld

C++ File Reading

Recommended Posts

How do you get file input stream to detect the end of a line?

If I have a file that looks like this:

Jim
80 90 100
Joe
70 80 90

And wanted output (cout) to look like this:

Jim: 90
Joe: 80

With those numbers being the averages of Jim's three and Joe's three respectively, how would I get the program to detect the new line? If there was some special character to denot the end of a set of numbers, I could do it, but there is not, so I cannot. :(

Share this post


Link to post
Share on other sites

Well \n is a newline character, so search for that I would guess (you could use strrchr for doing something like that).

 

Otherwise, why not use fgets? :)

 

"

The fgets() function reads at most one less than the number of characters

specified by size from the given stream and stores them in the string

str. Reading stops when a newline character is found, at end-of-file or

error.

"

Share this post


Link to post
Share on other sites

another way to do it is

 

#include <fstream.h>

 

 

 

ifstream datafile;  
datafile.open("a:gradesin.dat");



for (i=0;!datafile.eof();i++)

 

This will read in one line at a time using a for loop, of course you can implement it in a while loop or whatever.

Edited by LobbDogg

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×