[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

URGENT - Variable length records

Posted on 1998-05-05
4
Medium Priority
?
185 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 60 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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.
Suggested Courses

834 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