Hi all,
I'm sitting and tring to read a matrix into a dynamic array. Does anyone have a smart way of doing this?
Thanks for oyur time.
Esben.
Hi all,
I'm sitting and tring to read a matrix into a dynamic array. Does anyone have a smart way of doing this?
Thanks for oyur time.
Esben.
I'm thinking of something like a loop. Maybe combine with some form of reading function...
Quzah.
Hope is the first step on the road to disappointment.
Sarcasm is always funny...
He actually gave you good advice. Let's see what you've done so far...Originally Posted by esben
- xeddiex
Hi.
Yeah I guess using a loop and a read function is a pretty good advice, since it's the only way (as far as I know) to read in a vector or matrix, but I was kind of hoping for some more specific info. This is how I ended up reading the input file:
I would like to make the array dynamic though...Code:int elements = 0; int i = 0; int j = 0; int DataMatrix[row][col]; char temp = 0; InFile = fopen("DWTinput.txt", "r"); //Read data into matrix: for(j = 0; j < col; j++){ i = 0; while( i < row){ n = fscanf(InFile, "%d", &temp); //If I use fgetc, the ascii values are returned. elements += n; DataMatrix[i][j] = temp; i++; } }
\Esben.
> I would like to make the array dynamic though
How dynamic?
Does the file contain the "row" and "col" values as the first two data items?
Or do we figure out how many rows and cols there are from just reading how many values there are on each line, and how many lines there are?
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.
>>Salem,
How dynamic... good point...
What I would like to do is to return the DataMatrix[][] variable from one function, back to an int pointer (int *p) in the calling function. Is this possible in any way or need I return it to an integer-array pointer( int (*p)[]). I am not very happy about the latter solution...
Hope this clearified my problem a bit.
\Esben.
Well assuming that the first two numbers are the dimentions...
You then have your file reading code as it stands, then finally youCode:int **DataMatrix; fscanf(InFile, "%d %d", &row, &col); DataMatrix = malloc ( row * sizeof *DataMatrix ); for ( i = 0 ; i < row ; i++ ) { DataMatrix[i] = malloc ( col * sizeof *DataMatrix[i] ); }
return DataMatrix;
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.
*mutters something about a search button and wanders off*
Quzah.
Hope is the first step on the road to disappointment.
Store the height, width and size in the file header.
Read in the file header using good old C I/O with _open, _read, etc.
Allocate memory for the array based on the info in the header.
Read in the data from the disk into the array using _read.
Close the file.
Thanks for your replies guys! All helpfull.
\Esben.