Solved

URGENT - Variable length records

Posted on 1998-05-05
4
178 Views
Last Modified: 2010-04-10
I need some help please.

I am reading a comma delimited file using the ifstream class.  Many fields are variable so I am using m_file.getline(a,80,","); to read the entry.  with a being previously defined as  char a[80];  This is not a problem and seems to work ok.  The problem is when I read the records the data is not being nulled when I do another read.  I have tried a=NULL, a[]=NULL to no avail.

So my question is:  if I decalre a variable with char a[80] how do I ensure that it is set to null after each read/processing cycle so that processing of further records is not interfered with because of trash data?

Am I using the correct variable type? or is there a better way to do this?

Hope you can help.
0
Comment
Question by:SteveWood
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 20 total points
ID: 1173058
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173059
You can fill the array (a[]) with NUL characters using the standard C function memset().  For example,

memset(a,0,80);

The second parameter is the byte value you want to set the array to and the third parameter is the length to set.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173060
An alternate procedure is to use one of the get() procedure (not getline()).  There are 4 or 5, but one of them takes a delimiter (like you need) and automatically terminates the string (which getline is not doing).  However, get will read past the end of the line.  In other words it will potentially read multiple lines until it finds the terminatory.  You might not want that.

The only other option is to write a procedure that does exactly what you need.  This procedure would read the data one character at a time (using get()) and then return the string you need properly terminated.  Often this is the best way to go.  It depends on how well the existng functions handle your needs.
0
 
LVL 1

Author Comment

by:SteveWood
ID: 1173061
Thanks I will try it out.  Sounds promising though.
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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

679 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