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

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.   Paste as plain text instead

  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.

Sign in to follow this  

×
×
  • Create New...