Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you copy a file into a 2-dimensional array?

Status
Not open for further replies.

UG

Programmer
Dec 2, 2000
1
0
0
US
Anybody know how to do that?
 
Can you be more specific?
What does the file contains?
 
You shuld be more specific on this.
Suppose if the file contains number like this,
1 2
3 4
5 6
You can use a loop like this.

while( !feof(fileptr))
{
fscanf(fileptr, "%d %d", &arr[0], &arr[1]);
++i;
}

I hope this helps.

Cheers,


Baskar
baskar52@hotmail.com
 
Basking got caught by the TGML processor which wiped out his 's ;) -- I added a little more detail to it his code as well:

#define NUM_ROWS 3
#define NUM_COLS 2

...

int i=0;
int arr[NUM_ROWS][NUM_COLS];

while(i<NUM_COLS && !feof(fileptr))
{
/* check here to make sure fscanf agrees with us */
if (fscanf(fileptr, &quot;%d %d&quot;, &arr[0], &arr[1])!=NUM_COLS) {
fprintf(stderr,&quot;Bad file\n&quot;);
break;
}
++i;
}

This is a naive implementation (the array is of a fixed size, limiting the # of columns and rows allowed) but since you didn't provide very much information on what you want to do, it's probably better left that way.

HTH,

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top