syntax error near unexpected token '(' in C

I'm getting a confusing error message. I'm running MinGW on Windows XP 32-bit. When I attempt to compile the following code, I get an error message "./hello.c: line 4: Syntax error near unexpected token '('". Line 4 is at int main(...), I can't figure out what unexpected token is "near '('". I've tried using int main(void), but I get the same message. However, if I compile it without the "char string..." and "data = fputs(...)" and have it read from a given text file, it compiles without issue. What I'm trying to accomplish is to read from a file where the filename is given by an external source, i.e. php. Eventually I'm going to be working this into an Apache module with a parser that I've made, hence the call from php, but I wanted to fool around and build some template code to work with before I got to that part. #include #include int main (void) { FILE *fp; //char string = "JD"; commented out char data; //printf("Type in your filename: "); also commented out //scanf("%s", &argv); also commented out if(argc >= 2) { fp = fopen("sample.txt", "r"); //switched to reading a given file } while((data = getchar()) != EOF) { fgets(data, sizeof(data), fp); // data = fputs(string, fp); } if (fp==NULL) /* error opening file returns NULL */ { printf("Could not open player file!\n"); /* error message */ return 1; /* exit with failure */ } /* while we're not at end of file */ while (fgets(data, sizeof(string), fp) != NULL) { printf(data); /* print the string */ } fclose(fp); /* close the file */ return 0; /* success */ } Okay, I tried writing a simple "Hello World" program, but I'm still getting the same error message with it which makes me think the error message isn't being caused by my code at all. #include int main(void) //still getting a syntax error before unexpected token '(' { printf("Hello, world!"); return 0; }
This fgets(data, sizeof(string), fp) provokes undeinfed bahaviour as reading to an invalid address, namely the value of data.

以上就是syntax error near unexpected token '(' in C的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏