GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Sep-2005, 23:37
ladyscarlet99 ladyscarlet99 is offline
New Member
 
Join Date: Sep 2005
Posts: 3
ladyscarlet99 is on a distinguished road
Exclamation

How to read unknown total of int data from text file to a 2-dim array in C or 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:

CPP / C++ / C Code:
#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.
Last edited by LuciWiz : 09-Sep-2005 at 00:07. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 09-Sep-2005, 05:48
Evil Requiem Evil Requiem is offline
New Member
 
Join Date: Aug 2005
Posts: 24
Evil Requiem will become famous soon enough
I'd just use this, but you seem to be programming in C so this might not be an acceptable solution:
Code:
std::vector< std::vector<char> > myVec;
  #3  
Old 09-Sep-2005, 14:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
First problem I see can be solved by reading this thread, 2nd post. It will help make sure you are reading the correct amount of data.

Second, the tutorial on code formatting is important. If your code is hard to read, the tendancy is to move to the next post. Also, the Guidelines are helpful for getting the code tags right.

Two possibilities:
1) create a 2D buffer of ints that can hold a maximum number of rows/columns that makes sense for your problem. I.e. creater a matrix of [100][100] if you know for sure there will never be that many values in your file. I see this is the solution you've started with. It is the easiest.
2) the hard way is to allocate a pointer to an array buffer. You will be dealing with pointers and pointers to pointers in this case, as well as allocating memory. If that's a scary concept, continue as you are. But if pointers are OK, let us know and we can help you with that solution.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:14.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.