Solved

Problem with getline function reading from file

Posted on 2003-11-06
2
1,139 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now