Solved

URGENT - Variable length records

Posted on 1998-05-05
4
176 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 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