468,456 Members | 1,630 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

Reading lines of integers from a file..

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
3 7673
Nath
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
The maximum number of elements per line would be 200.
Jul 6 '06 #3
Banfa
9,051 Expert Mod 8TB
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.

Similar topics

reply views Thread by Cormac O'Donnell | last post: by
2 posts views Thread by adpsimpson | last post: by
8 posts views Thread by mahurshi | last post: by
6 posts views Thread by bas | last post: by
7 posts views Thread by fakeprogress | last post: by
10 posts views Thread by Tyler | last post: by
3 posts views Thread by The Cool Giraffe | last post: by
4 posts views Thread by C++ Newbie | last post: by
1 post views Thread by payalp | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.