i need help with reading from a text file

hi there,
i have a homework to do, write c++ code for union find..
in order to do that i have to read data from a text file that is generating 600000+ of random integers...
here is my file:

https://dl.dropboxusercontent.com/u/76524703/data-for-union-find.txt

here is the thing i need to save the content of the text file in an array so i can implement the union find function..
the instructor said the first integer in the text file generated is the size of the array we should use. now here is my code:
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
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	fstream input;
	input.open("data-for-union-find.txt");

	int a;
	input>>a;

	int size;
	size=a;
	
	int *data;
             data= new int [size];

for ( int i=0;i<size;i++)
{
	input>>data[i];
}

cout<<data[size-1];

}


my problem is after checking the last integer in the array by cout<<data[size-1];

i found out that this number is found somewhere in the middle of text file and its not saving all the text file in the array..

is there any other method to find out the max number in the text file so i can create the proper array size ??
the instructor said the first integer in the text file generated is the size of the array we should use
He did not tell you that whole file should be read.

If you still insist on reading whole file, you can:
1) Overallocate your array to some very large number (uneffective, dangerous)
2) Use automatically groing container as std::vector (most effective)
3) Write code to grow array when needed asyou input data (complex, nedd to be careful)
4) Do two passes on a file: first to count numbers, second to read them.
no im 100% positive that i should read all the file.. now i went on over the code that is generating the random numbers and it turned out that the first integer is the maximum numbers and the rest is being written in form of x,y for ex:


size
X Y
X Y
X Y
X Y
etc..

so i edited my code:
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

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	fstream input;
	input.open("data.txt");

	int a;
	input>>a;

	int size;
	size=a;

	int *x;
	int *y;
x= new int [size];
y=new int [size];


for ( int i=0;i<size;i++)
{
	input>>x[i];
	input>>y[i];
}



cout<<x[size-1]<<" "<<y[size-1];



}


but now im getting numbers that dont exist in the text file.. im confused..

Is these numbers 172553 425102? if so, it is all right. Your file says there are 60000 entries but there are 1620000 lines
i wish i'm getting something like this: -4590280 -4590280

if i post the original code of the generate data can you know whats going wrong ?
Do your file really called data.txt? Check if file is open before using it. You use same file as you posted in first post?
yeah my text file is in the same folder as of the project... and i've changed the
data-for-union-find.txt
to
data.txt

i thought there was a problem with the name or something..
Check if file is open anyway
it is open i checked it..
the problem is with the size..
here the instructor code for generate data file text.. if you can figure out the size:
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
#include <ctime>
#include <iostream>
#include <fstream>
using namespace std;
typedef unsigned __int32 uint32_t;
/***** SET VALUE IN seed variable BELOW  ***/

 uint32_t seed=7;

/*************************/

uint32_t my_rand(){

 int mod=0xffffffff;
 int a=1103515245;
 int c=12345;
 
 seed=(seed*a+c)%mod;
 return seed;
}

int main()
{
ofstream os;

os.open("data-for-union-find.txt");

// max used in Spring 2014
int max=600000;

int num_edges=2*max+seed*max/20;
int x,y;

    os<<max<<endl;

   for(int i=1;i<=num_edges;i++){
     x=my_rand()%max;
     y=my_rand()%max;
     os<<x<<" "<<y<<endl;
   }
os.close();
}


thank you...
Amount of entries is num_edges, not max. Max is the maximum possible value.

it is open i checked it..
I would like to see that in code.
Topic archived. No new replies allowed.