c - Basic file I/O of a ppm file -
i'm trying write program read through .ppm file , keep data in struct. able though, need able open file, not working far. i'm doing wrong. can please take @ code , see if can find out what's 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 open file %s\n", filename); exit(1); } fscanf(pfile, "%d", &x); fclose(pfile); printf("%d\n", x); return 0; }
this gives me "\n" on stdout. should fscanf array rather int?
based on feedback edited code scanning 2 chars:
int readfile(char *filename) { char first, second = 0; file *pfile; pfile = fopen(filename, "rb"); if(!pfile) { fprintf(stderr, "unable 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; }
according http://en.wikipedia.org/wiki/netpbm_format file starts 2 byte sequence: p1
, p2
, p3
(as human readable text - ascii). reading int
not work. should read in char
(for p
) , char
number , find out format file is. depending on format take further steps.
Comments
Post a Comment