Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 9th, 2005, 12:41 AM   #1
ladyscarlet99
Newbie
 
Join Date: Sep 2005
Posts: 3
Rep Power: 0 ladyscarlet99 is on a distinguished road
Red face How to read unknown total of int data from text file to a 2-dim array in C

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.
ladyscarlet99 is offline   Reply With Quote
Old Sep 9th, 2005, 12:43 AM   #2
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Sep 9th, 2005, 2:28 AM   #3
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
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 2:48 AM.
stevengs is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC