Solved

Problem with getline function reading from file

Posted on 2003-11-06
2
1,144 Views
Last Modified: 2010-04-02
Here is the problem.  When I execute the code contained in the zip file at http://www.theotron5000.com/help.zip (my personal website) the text file is not parsed properly.  I use a series of nested loops to read in to a series of integers and character array variables (see source).  The parsing works properly after the first line; however, the first line is not read in entirely properly.

For instance, for the text files included in the .zip archive, the first two lines of text input are:

1,6,Uno Mas,1,iiii,1,jjjj,2,kkkk,,,,,,,,,,,,,,,,,1,mmmm
2,7,Jackson,2,iiii,4,jjjj,5,kkkk,,,,,,,,,,,,,,,,,6,mmmm


These result in the output of:

1,6,Uno Mas,1,,1,jjjj,2,kkkk,0,,0,,0,,0,,0,,0,,0,,0,,1,mmmm
2,7,Jackson,2,iiii,4,jjjj,5,kkkk,0,,0,,0,,0,,0,,0,,0,,0,,6,mmmm

NOTE: in the above output, the strin 'iiii' is missing from immediately after the '1' following 'Uno Mas'.  However, in the second line that string appears correctly.  Also, the text file being read is NationalReps.txt, the others may be ignored.

Why is this occurring?

Thanks for the help,
Theo Peterson
theo@iastate.edu
0
Comment
Question by:theotron5000
2 Comments
 
LVL 15

Accepted Solution

by:
efn earned 500 total points
ID: 9697374
Here are the key lines:

char na_temp[12][256];

for(i = 0; i < 150; i++)
{
natFile.getline(na_temp[i],256,',');

As soon as i > 11, this writes outside the bounds of the array.

--efn
0
 

Author Comment

by:theotron5000
ID: 9698234
I am a moron.  Thanks for pointing out the obvious error efn.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question