Link to home
Start Free TrialLog in
Avatar of SteveWood
SteveWood

asked on

URGENT - Variable length records

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.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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.
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.
Avatar of SteveWood

ASKER

Thanks I will try it out.  Sounds promising though.