473,414 Members | 1,700 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,414 software developers and data experts.

Reading files

34
Hi all,

I made the following loop to read data from a text file:

Expand|Select|Wrap|Line Numbers
  1. inStream.getline(name, 25, '\n');
  2.         while(!inStream.eof())
  3.         {
  4.             inStream.getline(address, 80, '@');
  5.             inStream >> yearofbirth;
  6.             //discard newline
  7.             inStream.get();
  8.             inStream.getline(telno, 15, '\n');
  9.             //discard newline
  10.             instream.get();
  11.             addRecord(name, address, yearofbirth, telno);
  12.             records++;
  13.  
  14.             inStream.getline(name, 25, '\n');
  15.         }
the txt file itself contains data in the following format:

name\n
address@ (can be more than one line)
yearofbirth\n
telno\n

name\n
address@ (can be more than one line)
yearofbirth\n
telno\n

name\n
address@ (can be more than one line)
yearofbirth\n
telno\n

etc....the '\n' and '@' at the end of each data is the deliminator between the four data. However, this code goes into an infinate loop for some reason. When it executes line 14, shouldn't the loop continue if there is a new data or get out of the loop since it should encounter the end of file? I tried using while (inStream.getline(name, 25, \n)) vs using .eof() but that resulted in only one set of data being read. Any help?

Regards,
Jthep
Dec 11 '07 #1
3 1971
weaknessforcats
9,208 Expert Mod 8TB
You have several calls to inStream.getline() and inStream.get(). Should any of these reach eof, or encounter some other error, a failbit is set. The inStream.eof() first checks the failbit and if it is set just returns without doing anything.

You should check each of your inStream calls for success.
Dec 11 '07 #2
jthep
34
so I should try something like:

Expand|Select|Wrap|Line Numbers
  1. bool exit = false;
  2.  
  3. while (!exit)
  4. {
  5.     inStream.getline(name, 25, '\n');
  6.     if(inStream.eof())
  7.     {
  8.         exit = true;
  9.         break;
  10.     }
  11.     inStream.getline(address, 80, '@');
  12.     if(inStream.eof())
  13.     {
  14.         exit = true;
  15.         break;
  16.     }
  17.     inStream >> yearofbook;
  18.     if(inStream.eof())
  19.     {
  20.         exit = true;
  21.         break;
  22.     }
  23.     inStream.get();
  24.       if(inStream.eof())
  25.     {
  26.         exit = true;
  27.         break;
  28.     }
  29.     inStream.get(telno, 15, '\n');
  30.     if(inStream.eof())
  31.     {
  32.         exit = true;
  33.         break;
  34.     }
  35.     inStream.get();
  36.     if(inStream.eof())
  37.     {
  38.         exit = true;
  39.         break;
  40.     }
  41.  
  42.     addRecord(name, address, yearofbirth, telno);
  43.     records++;
  44. }
What should I use, break, continue, exit?
Dec 11 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
break and contuinue are forms of go-to programming. In the one case, you go-to the end of the loop and in the other case you go-to back to the start of the loop.

I personally don't like that style but you cna use it.

I prefer your exit approach where the bool is tested on each cycle of the loop.
Dec 11 '07 #4

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

Similar topics

3
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
2
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
10
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...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
4
by: Miner Jeff | last post by:
Hello, I have a basic question about reading files. I have several data files where the filenames are identical except for a short (3 character) prefix. I inherited this code and the person who...
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
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...
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,...
0
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
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...

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.