I wonder could anybody help me reading in a file to a 2d array
File rates.txt contains:
1.00,0.99,0.64,121.42,4.65
0,1,2,3,4
I am new to c so any help would be much appreciated.
I wonder could anybody help me reading in a file to a 2d array
File rates.txt contains:
1.00,0.99,0.64,121.42,4.65
0,1,2,3,4
I am new to c so any help would be much appreciated.
What have you tried so far? Post some code and someone will help.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
It's the same as reading to a 1d array...
File >> array[i][j];
Where File in the file you have opened for input, i and j are integers that are less that the array size( if that's what you mean )
And every thing else depends on the format of the input file, you have to know the format then, try to read from it.
none...
File >> array[i][j];
Looks a bit too C++ like for the C board !
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
As you can see I don't really have a clue how to read in the file. Do you need to use fscanf twice for each line in the file?
Anyhow below is my miserable attempt:
<code>
#include <stdio.h>
FILE *fp;
int main(void)
{
float array[5][5];
int i,j;
if ((fp=fopen("rates.txt", "w+")))
{
fprintf(stderr,"cannot open %s\n","fp");
}
while(feof(fp)==0)
{
fscanf(fp, "%f,%f,%f,%f,%f\n%d,%d,%d,%d,%d", array[i][j]);
for (i=0; i < 5; i=i+1)
for (j=0; j < 5; j=j+1)
printf("\n array[i][j]");
}
return 0;
}
</code>
Sorry, you are right...Originally posted by Hammer
File >> array[i][j];
Looks a bit too C++ like for the C board !
none...
What seems to be happening is its only printing
array[i][j] all the ways down the screen.
I want it to print the contents of the file:
e.g
0,1.00
1,0.99
etc
etc
etc
any ideas?
>>any ideas?
Yes, read up on how to use printf() properly.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]