I can't find my segmentation fault
int main(int argc, char *argv[])
{
int * WIDTH = 0, * HEIGHT = 0;
FILE *inFile;
inFile = fopen(argv[2],"r");
Header(WIDTH, HEIGHT, inFile);
The above is the beginning of my main function. It goes on, but I don't
believe the problem lies after this. Below are the other needed functions
for the Header function.
void Header(int *w, int *h, FILE *in) {
int V;
char ch1;
char pNum;
ch1 = fgetc(in);
pNum = fgetc(in);
if (ch1 != 'P' || pNum != '6') {
fprintf(stderr, "Needs to be a P6.\n");
exit(1);
}
spaces(in);
fscanf(in, "%d", w);
spaces(in);
fscanf(in, "%d", h);
spaces(in);
fscanf(in, "%d", &V);
spaces(in);
}
void spaces(FILE *in ){
unsigned char ch2;
ch2 = fgetc(in);
while (ch2 == '#' || isspace(ch2)) {
// if the character is a comment, skip the line
if (ch2 == '#') {
while (ch2 != '\n') {
ch2 = fgetc(in);
}
}
else {
// skip whitespace
while (isspace(ch2)) {
ch2 = fgetc(in);
}
}
}
// put back extra character
ungetc(ch2, in);
}
Can anyone point out what is wrong for me? I keep getting a segmentation
fault. It will compile, but whenever I try to run it with a .ppm image, I
get a seg fault. I think it has to do with the fopen function, but I'm not
sure. Can anyone help? Sorry, I'm new to C so if there are any glaring
mistakes in how I code, please forgive me.
EDIT: I ran gdb. I get an error that goes:
0 _IO_get c (fp=0x0) at getc.c:40
1 0x08048768 in readHeader ()
2 0x08048702 in main ()
//////////////
Also guys, if possible, could you just point me in the right direction
(pun intended ;) ). I really want to learn this on my own.
No comments:
Post a Comment