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
Solved

Problem with getline function reading from file

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 75
Find Visual Studio Tools 2 111
C++ question 3 70
Eclipse IDE - Cannot copy/paste from console output 8 253
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

840 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