Solved

Problem with getline function reading from file

Posted on 2003-11-06
2
1,147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.
Suggested Courses

623 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