Click to See Complete Forum and Search --> : Reading files in C


Concrete Geist
09-13-2001, 08:12 PM
My painful lessons continue in C, and I have more problems. Reading files. Here is the code :
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,c,d,e;
FILE *fp;

if((fp=fopen("read.txt", "r"))==NULL)
{
fprintf(stderr, "Error reading file\n");
exit(1);
}

else {
fscanf(fp, "%d %d %d %d %d", a,b,c,d,e);
printf("The values are %d %d %d %d %d\n", a,b,c,d,e);

fclose(fp);
}
return 0;

}

:mad: urg, I suck.

Concrete Geist
09-13-2001, 08:47 PM
*cough* *cough*

nm, I see it. This is embarrasing. :rolleyes:

I'll post future disk file problems in this thread to avoid wasting everyones time.

jemfinch
09-14-2001, 01:05 AM
Forgot to use the address-of operator (&) in fscanf?

Jeremy

MrNewbie
09-14-2001, 09:22 AM
Yeah jemfinch is right, without the address of operators, a .. e variables aren't initialized and so compile errors are returned.
fscanf() takes pointers to the variables. Change the line to this:

fscanf(fp, "%d %d %d %d %d", &a,&b,&c,&d,&e);