Function needs to create array from file.

I need to create a function that reads the contents of a file and creates an array from them. I need a counter that tells me the number of numbers on the file so I can use it for the size of the array. Here is where i am stuck, how do I use that counter since the size of the array has to be a constant number, and the counter isnt.
This is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void readFile()
{
	
	int number;
	int count = 0;
	ifstream  infile;
	infile.open("number.txt");
	if(!infile)
		cout <<"Error";
	else
	{
		while(infile>>number)
		{
			count++;
			cout<<number<<endl;
		}

	}
	
	for(int i = 0;i<count;i++)
	{
		
	}
}
You have a couple of options:

1) Best option, use a vector.
http://www.cplusplus.com/reference/vector/
A vector will resize itself automatically as you add entries.

2) Make two passes of the file.
Use the first pass to count the elements.
Allocate your array using
1
2
3
int * array;
...
array = new int [count]

Use the second pass to read into the allocated array.
Don't forget to delete the array when you're done.
Thanks, I tried what you said but I must be doing something wrong cause it still is not working. Maybe I wrote it incorrectly somehow. Here is what i have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void readFile()
{
	int* array;
	int number;
	int count = 0;
	ifstream  infile;
	infile.open("number.txt");
	if(!infile)
		cout <<"Error";
	else
	{
		while(infile>>number)
		{
			count++;
		}
	}

	array = new int[count];
	
	
	for(int i = 0;i<count;i++)
	{
		while(infile>>number)
		{
			array[i]= number;
			cout<< array[i]<<endl; //cout to see if it worked (it didn't)
		}
	}
	
	delete [] array;
	array = NULL;
}
ok scratch everything I just said.

infile.open("number.txt");
if(!infile)
cout <<"Error";
else
{
while(infile>>number)
{
count++;
}
}

change that to this:

infile.open("number.txt");
if(infile.is_open()) // returns true if file is open
{
while(infile.good()) // loop will continue until eof is reached
{
infile>>number;
count++;
}
}
else
{
cout <<"Error";
}
}

not sure if the infile>>number thing was exactly what you wanted to do there but anyway..
Last edited on
@clarkkent
After the first pass through the file, the stream will be in a failed state, since the end of file was reached. You need to do two things: clear the error flags, and reset the read pointer back to the start of the file.
1
2
    infile.clear();
    infile.seekg(0,ios::beg);


Also, on the second pass, use either a while-loop or a for-loop, but not both. Currently, all the numbers would be stored in array[0].


Having said that, I'd use a vector instead, if I was given the choice.
Last edited on
@cPlusN00b
I don't particularly recommend this code, as the count gets incremented regardless of whether or not the file input was successful.
1
2
3
4
5
while (infile.good()) 
{
    infile>>number;
    count++;
}
Thank you both, clearing and reseting worked.
The problem I guess now is that our professor told us that we could only use what we learn in class for our projects, and we have not touched on the clearing and resseting part. It sucks.
As for vectors he said we would not get into vectors so thats another thing I cant use. :\
Is there another way to approach this without making two passes of the file?
I would guess your professor would accept you making an array much larger than you would ever use.

In order to do the "two pass" you would also need to dynamically allocate the array.
You could use a separate ifstream (e.g. infile2) to open and read the file for the second pass, that way the flags and position will be in a fresh state.
Topic archived. No new replies allowed.