c0ld Posted December 14, 2004 Posted December 14, 2004 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 More sharing options...
markiemrboo Posted December 14, 2004 Posted December 14, 2004 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 More sharing options...
LobbDogg Posted December 15, 2004 Posted December 15, 2004 (edited) 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 December 15, 2004 by LobbDogg Share this post Link to post Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now