Solved

URGENT - Variable length records

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
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.

757 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

20 Experts available now in Live!

Get 1:1 Help Now