473,490 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading lines of integers from a file..

2 New Member
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 7827
Nath
1 New Member
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
adamatbu
2 New Member
The maximum number of elements per line would be 200.
Jul 6 '06 #3
Banfa
9,065 Recognized Expert Moderator Expert
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

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

Similar topics

0
4064
by: Cormac O'Donnell | last post by:
I am implementing an fft algorithm in java and would like to clear this point up. I am reading a 16 bit wav file with format : float sampleRate = 16000; //8000,11025,16000,22050,44100 int...
2
5203
by: adpsimpson | last post by:
Hi, I have a file which I wish to read from C++. The file, created by another programme, contains both text and numbers, all as ascii (it's a .txt file). A sample of the file is shown below: <<...
8
3431
by: mahurshi | last post by:
i have a simple question guys i have to read a file that looks more or less like this AND 1 2 3 OR 2 3 4 INV 5 2 BUF 7 1 AND 1 2
6
1763
by: bas | last post by:
hey, I am having a lot of trouble trying to get this program to work properly. It is supposed to read integers from a file and place them into the categories for inventory. The problem seems to...
7
2924
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
10
8329
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
3
2804
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
4
3741
by: C++ Newbie | last post by:
Suppose I have a text file with the input: 1 2 3 4 5 6 7 8 9 10 ! Comment: Integers 1 - 10 How do I write a C++ program that reads in this line into a 10-element vector and ignores the...
1
1911
by: payalp | last post by:
(1) Reads from the file “hw4.txt” in the local directory the SID (which is a string) and the scores (which are integers) of each student in a class. Although the number of students and the number of...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.