473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading numbers from a text file problems

Hey guys, I have another problem in an assignment. I'm supposed to be reading a list of numbers seperated by one blank space spanned across three lines.

I'm having trouble figuring out how to skip blank spaces and just grab the number in each pass of the loop. A lot of this assignment's loops get averages of sets of these numbers. Originally I had it in:

Expand|Select|Wrap|Line Numbers
  1. char fileinput;
  2. int sum
  3. int count
  4. float average
  5. ...
  6.  
  7. while(!filein.eof())
  8. {
  9. finein.get(fileinput);
  10. sum = sum + fileinput;
  11. count = count + 1;
  12. }
  13.  
(That was typed up here as an example, and isn't a copy/paste of my actual code.)
Pretend filein is datfile1.txt and datfile1.txt equals

517 977 546 178 11 33 71 \n
716 333 811


(The real thing is different, and I've tried having a numinput int and a blankspace char, but it kicks back errors.)
Mar 1 '08 #1
5 2183
Laharl
849 Expert 512MB
First off, input is generally delimited by whitespace, so you don't really need to account for it in this manner. Also, there are better functions to use than get(). If you're working in C, read this. If you're working in C++, read this instead.
Mar 1 '08 #2
First off, input is generally delimited by whitespace, so you don't really need to account for it in this manner. Also, there are better functions to use than get(). If you're working in C, read this. If you're working in C++, read this instead.
so filein >> num when num is an integer variable would get those numbers one after the other?
Mar 1 '08 #3
Laharl
849 Expert 512MB
Yep, you've got the right of it, as long as file is an ifstream or an fstream opened in read or read-write mode.
Mar 1 '08 #4
Alright, I got that much, but I have to keep the original formatting of the input file as well, including the lines. What would I use to do that so I don't get a bunch of jumbled together integers?
Mar 1 '08 #5
Got that, thank you for your help! Onto finding the range of these numbers.. *Remembers bubble sort from previous class and starts googling.*
Mar 2 '08 #6

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

Similar topics

3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
9
by: EkteGjetost | last post by:
I would like to first apologize to those of you who read my last post "desperately need help". As a regular on other forums i can understand how aggravating it would be to have someone come on who...
3
by: Millennium Falcon | last post by:
Hi! Help is kindly requested in reading floating point numbers from a text file. File is organized like this : 2000 // number of data -1.00000 -2.000000 -0.008944 // x, y & z-coordinates to...
9
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
10
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
6
by: Nick | last post by:
Hiya, Am having a few problems with StreamReader. I'm trying to open a text file, and read 5 columns, and put them into an array, and then do a calculation and read the next line of data, and...
32
by: Bill Cunningham | last post by:
I am interested in writing a numeric text reader. This only reads numbers of securities and stores them. Nice practice. I have determined that these functions are needed. isalpha, isdigit,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.