Problem with fscanf loop!
Hi all,
Apologies for posting a thread so soon after finding an answer to my last one. This little snippet of my code is driving me crazy.
Soooooo I dont understand the output for my code. Right now my text file is like this.
20010929 0 1
20010929 15 2
20010929 30 3
20010929 45 4
20010929 0 1
20010929 15 2
20010929 30 3
20010929 45 4
20010929 0 1
20010929 15 2
20010929 30 3
20010929 45 4
So with my code I'm hoping to put it into an array. This is just a pratice .txt as the one i will be using is 330770 rows long. When i display the 2nd row, 3rd column. The program displays a 45 instead of a 2. Any help or correction in understanding would be much appreciated. I'll add that it was working fine until I started to use malloc to define my array.
Code:
FILE *fr; // declare the file pointer
int x, y; // Dimensions of .txt data, 3 columsn and 330770 inputs;
//DYNAMIC MEMORY ALLOCATION FOR data ARRAY and finalData ARRAY.
int** data;
data = (int **) malloc(330770 * sizeof(int*));
for (int h = 0; h < 330770; h++)
data[h] = (int*) malloc(3*sizeof(double));
//determines the placement of the finalData
fr = fopen ("c:\\blahblah.txt", "r"); /* reads the file */
for(x=0; x<330770; x++){ // loops through the rows
y = 0; //starts at first column during each row loop
while(fscanf(fr,"%d", &data[x][y++]) >0); // loops through the columns until each row ends.
}
cout<<data[1][0];