By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Reading lines of integers from a file..

P: 2
I may be completely missing something here, but I'm trying to figure out how to read in floating-point numbers from a file (multiple lines of floats,) then sort the individual lines and output them to another file in sorted order, still separated by line. The sorting isn't a problem for me, but there's no end of line notification, correct? The only thing I could think of to do was to read it in as a string using getline and then try to convert it back with istringstream, but when I did this, it only worked for the first line of data. Any help would be greatly appreciated.

Input file example data:

1 4 3 2 9.5 8.2 120 <------ want to read in individual lines
3 5 22.0 4.4 86 <-------
99 33.6 388 11 31.5 <--------
Jul 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: 1
Do you know how many elements in a row before reading the file? if element count is differ from line to line, do you have any idea about the maximum number of elements out of all lines.

I may be completely missing something here, but I'm trying to figure out how to read in floating-point numbers from a file (multiple lines of floats,) then sort the individual lines and output them to another file in sorted order, still separated by line. The sorting isn't a problem for me, but there's no end of line notification, correct? The only thing I could think of to do was to read it in as a string using getline and then try to convert it back with istringstream, but when I did this, it only worked for the first line of data. Any help would be greatly appreciated.

Input file example data:

1 4 3 2 9.5 8.2 120 <------ want to read in individual lines
3 5 22.0 4.4 86 <-------
99 33.6 388 11 31.5 <--------
Jul 6 '06 #2

P: 2
The maximum number of elements per line would be 200.
Jul 6 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
having constructed you input stream you could just use the >> operator as in

Expand|Select|Wrap|Line Numbers
  1.   double d;
  2.  
  3.   MyIStream >> d;
  4.  
However this is not very robust to unexpected data so alternitively having called getline I would call strtod

double strtod ( const char * string, char** endptr );

repeatedly to extract doubles from the line or catch errors.
Jul 10 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.