reading .txt files

hello, I was wondering if anyone could look at this code for me and tell me what I need to do to get the array working. My problem is that I am reading a text file and I need to make an array that will size up to the text file while we do not know how big it is. What am I doing wrong here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	ifstream infile;
	ofstream outfile;
	int x = 100000;
	int num[x];

	//open a data file, set infile to read from it
	infile.open("random1000.txt");
	outfile.open("sorted.txt");

	while (!infile.eof())
	infile >> num[x];
	x++;

	//if you are a good programmer
	//you will always remember to close
	//your files (even though it doesn't matter)
	infile.close();
	outfile.close();

	return 0;
}

Last edited on
I tried this to see if I could get it to work but I can't even get that running. I need an array that will size up to the .txt file
Your while loop is wrong. It will only execute ONE instruction unless you put them in a block { ... }

Also you should not loop on infile.eof(). The EOF flag is not set until *after* you attempt to read the file. This would be better:
 
while(infile >> num[x]) ++x;

Or even:
 
while(infile >> num[x++]);


Then you need to deal with the problem of how big to make your array. The options are:
1) count the numbers in the file *before* reading them in.

2) resize your array as you go
Last edited on
I tried updating it to this, while I was away for a bit. How does this look?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	ifstream infile;
	ofstream outfile;
	int *a, x;

	//open a data file, set infile to read from it
	infile.open("random1000.txt");
	outfile.open("sorted.txt");
	a = new int[x];

	while (!infile.eof())
	{
		infile >> a[x];
		x++;
	}

	infile.close();
	outfile.close();

	return 0;
}

Galik, can you clarify a bit on the eof flag? i don't really understand what you mean on that.
x doesn't have a value.

I suggest you count how many numbers are in your file before creating your array. Then read the file a second time putting the numbers into the correctly sized array.
EOF:

EOF is only set *after* you try to read past the end of the file. It is not set simply by reaching the end of the file. So by testing for eof() before you do the read >> you don't know if the read >> was successful or not.
This time I know the size yes but how would I make it to where I can make an array if say I don't know the size.
You can count the size by reading in the file once and adding up how many there are before reading it in a second time.
I see
Thats a good point. Its one of those posts that I read once and didn't get so I read it again and I was like, ok!
I tried this but it keeps telling me something about a Access violation writing location 0x0000000000
First-chance exception at 0x5bcf2258 (msvcp100d.dll) in Searching.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x5bcf2258 (msvcp100d.dll) in Searching.exe: 0xC0000005: Access violation writing location 0x00000000.


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
int main()
{
	ifstream infile;
	ofstream outfile;
	int *a = NULL, x = 1000, y = 0;

	//open a data file, set infile to read from it
	infile.open("random1000.txt");
	outfile.open("sorted.txt");

	while(!infile.eof()) // To get you all the lines.
        {
	        infile >> a[x];
	        y++;
        }

	a = new int[x];

	//while (!infile.eof())
	//{
	//	infile >> a[x];
	//	x++;
	//}

	infile.close();
	outfile.close();

	return 0;
}

Last edited on
where are your files located? Are they in the correct folder?
Thats what I think, they are in the same folder where the .cpp and .h files are.
I keep trying and trying to get it to work but no luck
You are reading into an array which you have not allocated. Just read into a single int while you are counting the numbers:
1
2
3
int n; // somewhere to read the numbers in to
int y = 0; // count the numbers
while(infile >> n) ++y;

Last edited on
This is my latest code, how would I write out to a file?

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
33
{
	ifstream infile;
	ofstream outfile;
	int *a = NULL, x = 1, y = 0;

	//open a data file, set infile to read from it
	infile.open("random1000.txt");
	outfile.open("sorted.txt");
	a = new int[x];

	while(!infile.eof()) // To get you all the lines.
        {
	        infile >> a[x];
	        y++;
        }

	a = new int[x];

	infile.open("random1000.txt");

	while (!infile.eof())
	{
		infile >> a[x];
		x++;
	}



	infile.close();
	outfile.close();

	return 0;
}
I got my updated code working now but how do I get the complier to read the random1000.txt file again but from the top again.
Here is my code, I need it to read the file once to see how many are there but then read it again to sort it into an array.
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
33
34
35
36
37
38
39
40
41
42
43
int main()
{
	ifstream infile;
	ofstream outfile;
	int *a = NULL, x = 1, y = 0;

	//open a data file, set infile to read from it
	infile.open("random1000.txt");
	outfile.open("sorted.txt");
	a = new int[x];

	while(!infile.eof()) // To get you all the lines.
        {
	        infile >> a[x];
			cout<<a[x]<<endl;
	        y++;
        }

	a = new int[y];

	x = 0;
	int b = 0;

	while (x<y)
	{
		infile >> a[b];
		b++;
		x++;
		cout<<a[b]<<endl;
	}

	mysterySort(a, y);

	for(int z = 0; z < x; z++)
	{
		outfile<<a[z]<<endl;
	}

	infile.close();
	outfile.close();

	return 0;
}

To start reading from the beginning again you have to move the read pointer to the beginning of the file:
 
infile.seekg(0);

and you need to clear the end of file error flag:
 
infile.clear();

Topic archived. No new replies allowed.