Y
yourmycaffiene
Okay, this if my first post so go easy on me plus I've only been using
C for a couple of weeks. I'm working on a program currently that
requires me to read data from a .dat file into a 2d array and then
print out the contents of the 2d array to the screen. I wil also need
to append data to the .dat file but mostly right now I'm worrying about
getting the info into the 2d array. My .dat file looks like this
1 20000
2 30000
3 40000
etc. The first number standing for an ID and the second standing for a
weight.
#include <stdio.h>
main()
{
FILE *pRead;
int array[20][3];
pRead = fopen("test.dat", "r");
if (pRead==NULL)
printf("\nFile cannot be opened\n");
else
printf("\nContents of test.dat\n\n");
fscanf(pRead,"%s,%s", array);
while ( !feof(pRead) )
{
printf("%s%s\n", array);
fscanf(pRead, "%s,%s", array);
}
}
I know I need to use a for loop or something to that idea so that I can
read in the column and then the row. I'm really drawing a blank here on
how to get that working. Could someone point me in the right direction?
Cause right now my output is coming out like this :
11
20002000
22
30003000
etc.
Also, if I use %d instead of %s here, I get segmentation faults. Can
anyone explain why this is happening? Thanks.
C for a couple of weeks. I'm working on a program currently that
requires me to read data from a .dat file into a 2d array and then
print out the contents of the 2d array to the screen. I wil also need
to append data to the .dat file but mostly right now I'm worrying about
getting the info into the 2d array. My .dat file looks like this
1 20000
2 30000
3 40000
etc. The first number standing for an ID and the second standing for a
weight.
#include <stdio.h>
main()
{
FILE *pRead;
int array[20][3];
pRead = fopen("test.dat", "r");
if (pRead==NULL)
printf("\nFile cannot be opened\n");
else
printf("\nContents of test.dat\n\n");
fscanf(pRead,"%s,%s", array);
while ( !feof(pRead) )
{
printf("%s%s\n", array);
fscanf(pRead, "%s,%s", array);
}
}
I know I need to use a for loop or something to that idea so that I can
read in the column and then the row. I'm really drawing a blank here on
how to get that working. Could someone point me in the right direction?
Cause right now my output is coming out like this :
11
20002000
22
30003000
etc.
Also, if I use %d instead of %s here, I get segmentation faults. Can
anyone explain why this is happening? Thanks.