I'm trying to write a program to read through a .ppm file and keep the data in a struct. To be able to do that though, I need to be able to open the file, which is not working so far. I'm obviously doing something wrong. Can you please take a look at the code and see if you can find out what's the problem?
#include <stdio.h>
#include <stdlib.h>
int readFile(char *filename);
int main(void)
{
readFile("myfile.ppm");
return 0;
}
int readFile(char *filename)
{
int x = 0;
FILE *pFile;
pFile = fopen(filename, "rb");
if(!pFile)
{
fprintf(stderr, "Unable to open file %s\n", filename);
exit(1);
}
fscanf(pFile, "%d", &x);
fclose(pFile);
printf("%d\n", x);
return 0;
}
This just gives me an "\n" on the stdout. Should I fscanf it into an array rather than an int?
Based on your feedback I edited the code to scanning to two chars:
int readFile(char *filename)
{
char first, second = 0;
FILE *pFile;
pFile = fopen(filename, "rb");
if(!pFile)
{
fprintf(stderr, "Unable to open file %s\n", filename);
exit(1);
}
fscanf(pFile, "%c%c", &first, &second);
fclose(pFile);
printf("First: %c, Second: %c\n", first, second);
return 0;
}