我收到一条令人困惑的错误消息。我在 Windows XP 32 位上运行 MinGW。当我尝试编译以下代码时,我收到一条错误消息“./hello.c: line 4: Syntax error near unexpected token '('". Line 4 is at int main(...), I can't找出什么意外标记是“靠近'('”。我尝试使用 int main(void),但我得到了相同的消息。但是,如果我在没有“char string...”和“data = fputs(...)" 并从给定的文本文件中读取它,它编译没有问题。
我想要完成的是从文件名由外部源给出的文件中读取,即 php.ini 文件。最终,我将使用我制作的解析器将其放入一个 Apache 模块中,因此是来自 php 的调用,但我想在进入该部分之前构建一些模板代码以供使用。
#include <stdio.h>
#include <stdlib.h>
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 */
}
好的,我尝试编写一个简单的“Hello World”程序,但我仍然收到相同的错误消息,这让我认为错误消息根本不是由我的代码引起的。
#include <stdio.h>
int main(void) //still getting a syntax error before unexpected token '('
{
printf("Hello, world!");
return 0;
}