![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 3
Rep Power: 0
![]() |
I want to create a function read data of type int from a text file and stores them in a 2-dimensional array. But the number of the data in the text file is unknown during the program execution. Therefore the size of the array is also unknown.
I have problems in reading the correct data value of type int and stores them in the array. I also can't determine the correct total of columns and rows for the array. My data in the text file looks something like this: 1111111111111111111 1000101010101010101 1010101010101011111 1111111111111111111 1011110000111111111 1111111111111111111 My coding is as below: #define ROWMAX 100 #define COLMAX 100 int data[ROWMAX][COLMAX]; int ROW=0,COL=0; void readdata(FILE *file,int data[ROWMAX][COLMAX]) { int c; int row=0,column=0,i,j; int temp[ROWMAX][COLMAX]; while(!feof(file)) { fscanf(file,"%d",&c); if (c!='\n' ) { temp[i][j]=c; //printf("%d",temp[i][j]); column=column+1; } else { //printf("\n"); column=0; row++; }//end if } //end while ROW=row+1; COL=column-1; printf("\nSize of array = [%d] x [%d]\n",ROW,COL); //display data for(i=0;i<ROW;i++) { for(j=0;j<COL;j++) { data[i][j]=temp[i][j]; printf("%1.4f",data[i][j]); } printf("\n"); } Does anyone knows the easiest solution to this? Please give me any complete source code examples for reading data of type int in text file and determining the total of rows and columns? Later, I need to scan each row of data for 0s and stores them in text file.then I access the text file to normalize all 0s by rows. Therefore I need to save the information about the location of the 0s(which rows and columns).After the normalization,I need to replace the 0s in the later text file with the new value at the correct location. How to save the location value(rows and pixels) and corresponds them with their new normalized value? Thanks in advance. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Please don't double post, and please put the code tags around your code.
Example [code_] fdgdgnmnmbv (code) [/code_] But take out the _'s
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
There are several possibilities. You could read the file twice, the first time assessing the dimension, then allocate an array according to the size you found, followed by a second read which fills your array (don't forget that you have to free this allocated memory later yourself). Another option might be to use a fully dynamic container ( vector, linked list, etc. ), in which case you would only have to read the file once.
(CODE tags, or PHP tags make the code much easier to read. You can find the appropriate buttons at the top of the Post editor) [PHP]#define ROWMAX 100 #define COLMAX 100 int data[ ROWMAX ][ COLMAX ]; int ROW = 0, COL = 0; void readdata( FILE* file, int data[ ROWMAX ][ COLMAX ] ) { int c; int row = 0, column = 0, i, j; int temp[ ROWMAX ][ COLMAX ]; while( !feof( file ) ) { fscanf( file, "%d", &c ); if ( c != '\n' ) { temp[ i ][ j ] = c; //printf( "%d", temp[ i ][ j ]); column = column + 1; } else { //printf("\n"); column = 0; row++; }//end if } //end while ROW = row + 1; COL = column - 1; printf( "\nSize of array = [%d] x [%d]\n", ROW, COL ); //display data for( i = 0; i < ROW; i++ ) { for( j = 0; j < COL; j++ ) { data[ i ][ j ] = temp[ i ][ j ]; printf( "%1.4f", data[ i ][ j ]); } printf( "\n" ); } [/PHP]
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Sep 9th, 2005 at 3:48 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|