i'm reading data from a file and putting it into a dynamic multidimensional array (pointer to an array of pointers to arrays). however i dont know how to pass the correct value to fscanf. any suggestions?
This is a discussion on passing ** value to fscanf within the C++ Programming forums, part of the General Programming Boards category; i'm reading data from a file and putting it into a dynamic multidimensional array (pointer to an array of pointers ...
i'm reading data from a file and putting it into a dynamic multidimensional array (pointer to an array of pointers to arrays). however i dont know how to pass the correct value to fscanf. any suggestions?
Code:int** p; int l, m; FILE * fp = fopen("data.txt","r"); // Make a 5 x 5 dynamic array p = new int*[5]; for( l = 0; l < 5; ++l ) p[l] = new int[5]; // Read data into dynamic array. for( l = 0; l < 5; ++l ) for( m = 0; m < 5; ++m ) fscanf(fp,"%d",&p[l][m]);
I used to be an adventurer like you... then I took an arrow to the knee.
hmm, debug assertion error
debug asks me to find fseek.c
wouldnt that code give the memory address of the pointer in the subarray of pointers?
It works for me, I created a test file "data.txt" containing 25 numbers as such:
Did you create a file to test with? You should also add code to check that the file has been opened properly. After reading in the data I was able to successfully print out all the correct values using another double for loop.Code:24 12 6 3 1 14 12 0 14 9 9 1 2 5 6 0 0 4 5 2 5 5 5 5 5
&p[l][m] gives the memory address of the individual elements of the subarray. Specifically, the address of element m in subarray l.Originally Posted by e_zealot
I used to be an adventurer like you... then I took an arrow to the knee.
> debug asks me to find fseek.c
Your code is broken somewhere in it's file handling, it's nothing to do with the way you allocated memory (if you did as already suggested by hk_mp5kpdw.
I suggest you post what you tried in its entirety.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.