Getting rid of junk at end of loops

wxjunkie

Senior member
Nov 6, 2000
409
0
0
In C++

I'm using various event controlled and counter controlled loops for reading in data from files, and storing them in arrays, etc, however, I always seem to get either one line with just a '0' or many lines of junk after the REAL data has been processed. It's almost as if these loops are going one too far.
 

TheDiggler

Senior member
Dec 23, 2002
695
0
0
Try posting a code snipet or a link to your code so we can see how you're reading data.
 

wxjunkie

Senior member
Nov 6, 2000
409
0
0


ifstream inFile;
inFile.open("input.txt");
int ctr=0;
inFile;
while (inFile) {
inFile >> cust[ctr].id >> cust[ctr].balance >> cust[ctr].quarter;
ctr++;
}
 

TheDiggler

Senior member
Dec 23, 2002
695
0
0
Try this:

ifstream inFile;
inFile.open("input.txt");
int ctr=0;

inFile; // What are you doing this call for???

// Change your while loop to continue until EOF, as follows:
while (! inFile.eof() )

{
inFile >> cust[ctr].id >> cust[ctr].balance >> cust[ctr].quarter;
ctr++;
}
 

wxjunkie

Senior member
Nov 6, 2000
409
0
0
I was using inFile; simply to test the file stream so while (inFile) would work (sentinel logic here)

I think my problem roots in the fact that I declare my struct array with a maximum size, so there actually isn't more being read than necessary, there's just memory addresses in the rest of the empty array index numbers.

So what's the best way to declare a struct array, and the read in as many lines as are in the file?
 

TheDiggler

Senior member
Dec 23, 2002
695
0
0
Now you're talking a completely different problem. Perhaps your original logic then was in fact readining the data correctly to the end of the file; however, if the size of your arrary is larger than the maximum # of elements you're reading, your problem can simply be solved by not using those array elements past the MAXIMUM ELEMENT # you've read in.

In reference to your question about how do you declare any array such that you only declare (or access) what you want, there are several ways of accomplishing this:

1) Don't use arrays, use Linked Lists instead. These can easily grow as you need them to.
-or-
2) a) Dynamically allocate the memory for the MAXIMUM # OF ELEMEMENT in your ARRAY OF STRUCTURES.
b) Once you've populated the array from the data file, re-allocate the array to be exactly the amount of memory required for the # of read in elements (this may require you to copy the old array to a new but smaller array).
-or-
3) Do step 2a. Then just keep track of your maximum USED element # and never reference the array(s) elements higher than that MAX USED ELEMENT #.
 

wxjunkie

Senior member
Nov 6, 2000
409
0
0
Okay so when the loop exits, the ctr value essentially is the size of the "used" portion of the array?

If I have my reading function in a file by itself, and it's a void function, what's the best way to allow the ctr variable to be used in other parts of my program?
 

Trevelyan

Diamond Member
Dec 10, 2000
4,077
0
71
AUGH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

My brain is going to explode from all this coding mumbo jumbo!!!!!!!!!!!!!!!!!!!!!!!!!!
 

TheDiggler

Senior member
Dec 23, 2002
695
0
0
Originally posted by: wxjunkie
Okay so when the loop exits, the ctr value essentially is the size of the "used" portion of the array?

If I have my reading function in a file by itself, and it's a void function, what's the best way to allow the ctr variable to be used in other parts of my program?

Yes, the ctr value is the size of the "used" portion of the array. In answer to your next question, let me pose a question to you: How are you making your arrays available to the rest of the program? Are you passing array references to your file read function? Are the arrays global variables? Are the arrays members of a some class? Whatever method you're using to make the arrays accessible to the rest of the program is IMO the way you should make a "used array size" variable available to the rest fo your program (i.e. set that variable equal to the value of ctr in your read function).
 

wxjunkie

Senior member
Nov 6, 2000
409
0
0
The arrays are in struct form and are defined in a header file, and declared in main. When I need to use them in other functions, I just pass the array to the function.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
I would suggest using an STL container for your data structures. It grows as you need it, size is readily available, etc.

Another bit ... testing for EOF often isn't enough if there is whitspace after the last line. I often put in some code to read out whitespace at the end of every loop.

So, maybe something like this:

vector<DataStruct> &cust;
DataStruct x;

while(!infile.eof())
{
inFile >> x.id >> x.balance >> x.quarter;
if(infile.good()) //only keep the data if the input stream is still in a good state
cust.push_back(x);

while(isspace(infile.peek()))
infile.get();
}


Something like that.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Another possibility is to use a library that addresses all of that stuff for you. For parameter input files, and command line parsing, I really like GetPot
 

TheDiggler

Senior member
Dec 23, 2002
695
0
0
Originally posted by: wxjunkie
The arrays are in struct form and are defined in a header file, and declared in main. When I need to use them in other functions, I just pass the array to the function.

I'm getting the impression you don't want to make major coding changes to your program. IMO, the quickest/dirtiest/easiest thing for you to do is to pass the USED ARRAY COUNT VALUE IN ADDITION to the ARRAY REFERENCE(s) in those functions you're reading from the array(s).

Given that your array(s) is/are declared in main, let me ammend a previous suggestion of mine. Have your FILE READ (i.e. ARRAY LOADER) function return the # of USED ELEMENTS in the array (i.e. ctr) instead of void. This way, in main itself you can assign a variable numArrayEntries equal to the return value of your FILE READ/ARRAY LOADER function.

Example:

#define MAX_ELEMENTS 65535

void main(void)
{
custStruct custArray[MAX_ELEMENTS];

// Here int variable numArrayElements is declared and populated w/ the result of loadArrayFromFile
int numArrayElements = loadArrayFromFile(custArray);

// Here function processArrayData is called. Notice that numArrayElements is passed to this function.
processArrayData(custArray, numArrayElements);
}