More issues...

Solve one problem, run into another... anyway I have another question. I am trying to write an array to a data file then read it and tell the size back. The creating array and saving it was easy. But I cannot get my function to read the array and return the size. Here is my code (call and function, respectively):
cout<< "Reading data from file\n";
ifstream inFile;
int datarray[100];
arraylength = readFile (inFile, datarray);
cout<< arraylength<<endl;

int readFile (ifstream &inFile, int datarray[])
{
int size;
inFile.open("num.dat");
if (inFile.is_open())
{
for (int i = 0; i < size; i++)
{
inFile >> datarray[i];
cout<< datarray[i];
}
inFile.close();
return size;
}
else
{
cout<<"Could not open.\n";
return -1;
}
}
size is defined, but not initialized. It could be any number it wants to be. You have to initialize it. I assume you want the size of the file?

This will give you some more insight, if that's the case:
http://www.cplusplus.com/reference/iostream/
Well actually, our teacher wants the number of elements in the array back. Which is stupid if you ask me since the function before this lets the user chose the size... but I am not a college professor.
That's beside the point. You declared int size; but you didn't do anything to it. So when you get to for(int i = 0; i < size; i++) you don't have any specific number of times that you're going to go through the loop. It's not storing any meaningful value.
Well, that is true. But then how can I create a counter that stops at the end of the array without it since I am not supposed to know it going in?
Do you have one int per line in the file? If so, then you can read the file until the end and count the number of lines.
Last edited on
size++;
Shacktar I think that would work. And ciphermagi, if I used size++ in that loop, wouldn't it be inifinite? O_o
Yes...but you're not supposed to use size as the limiter. That's the whole point.
Actually, it would be better if you did for(size = 0; inFile.eof() == false; ++size)
is .eof() end of file? We haven't learned very much about files yet so excuse my lack of syntax.
I'm almost positive that the syntax that I posted is syntax that needs you to research it more, but yes, eof means 'end of file'
Well I was looking for that and a came across feof but that requires pointers... which I am not allowed to use yet because our class hasn't gone over them.
I'm positive that there are eof functions that you can use to determine the end of a file. You may want to consider ditching the whole idea of a for loop, and use some other type of iteration.
Well there is also the problem of finding the sum and average. I have functions that take each element of the array to find sum and average, but how do I get them out of the file and transfer them to these functions?
Look at that link I gave you up there.
Test for .eof() before you display... otherwise, your final output will be an unintended result. I might do something like this:

1
2
3
4
5
6
7
8
9
10
int size=0;

while(size<100) { //your array size is 100 (index: 0-99)... protecting from overrun

     inFile >> datarray[size]; //read file
     if(!inFile) break; //essentially, read into the array until .eof() or other failure

     cout << datarray[size]; //display what you read
     size++;
}
Last edited on
That worked! Thank you. That really helped me understand how to end the file. However, when I need to get all the array elements out to add, would I need a readFile function similar to pull out each element for addition? I have to have this function, with these inputs:

int computeSum (int num[], int size)
{
int total = 0;
for (int i = 0; i < size; i++)
total = total + num[i];
return total;
}
Last edited on
No, you should be able to pass the array by reference in your readFile function.

Change this:
int readFile (ifstream &inFile, int datarray[])

To this:
1
2
//you must enclose the reference parameter and name in (), and you must specify size
int readFile (ifstream &inFile, int (&datarray)[100])

This will fill the array for use outside of the readFile function, AND get the size, all in one shot!
Last edited on
Haha thank you! Kill two birds with one stone.
Topic archived. No new replies allowed.