语法错误接近意外的标记'('在C中)。

时间:2023-01-30 22:45:20

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.

我得到了一个令人困惑的错误信息。我在Windows XP 32位上运行MinGW。当我尝试编译下面的代码时,我得到一个错误消息。c:第4行:意外标记附近的语法错误。第4行在int main(…)中,我不知道“near”(“”)是什么意外的标记。我尝试过使用int main(void),但我得到了相同的消息。但是,如果我不使用“char string…”和“data = fput(…)”编译它,并且从给定的文本文件中读取它,那么它就会毫无问题地编译。

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.

我想要完成的是从一个文件中读取文件名由外部源(即php)提供。最后,我将使用我所创建的一个解析器,将其工作到一个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 */
}

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.

好吧,我试着编写一个简单的“Hello World”程序,但是我仍然得到了相同的错误信息,这让我觉得错误信息根本不是由我的代码造成的。

#include <stdio.h>

int main(void) //still getting a syntax error before unexpected token '('
{
    printf("Hello, world!");
    return 0;
}

4 个解决方案

#1


0  

Your line

你的线

int main (int argc, char *argv)

is wrong. It must be

是错误的。它必须是

int main (int argc, char *argv[])

OR

int main (int argc, char **argv) //less common, though

And also your line

你也行

char string = "JD";

should be

应该是

const char *string = "JD";

Also, I don't get

另外,我不明白

scanf("%s", &argv);

why would you read something INTO argv?

你为什么要读到argv?

#2


0  

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 1024  // a normal buffer size for fgets
int main (int argc, char *argv[])
{
    FILE *fp;
    //char string = "JD";
    //char data;
    char buffer[MAXLINE];
    if(argc != 2)
    {printf("usage : ./a.out <filename>\n"); return -1;}

    if((fp = fopen(argv[1],"r+")) == NULL)
    {printf("open file %s error\n",argv[1]); return -1;}

   /*
    while((data = getchar()) != EOF)
    {
        fgets(data, sizeof(data), fp);
        data = fputs(string, fp);
    }*/      //I don't understand this part .


    /* while we're not at end of file */
    while (fgets(buffer,MAXLINE , fp) != NULL)
    {
        printf("%s\n",buffer); /* print the string */
    }

    fclose(fp); /* close the file */
    return 0; /* success */
}

#3


0  

After letting my computer rest for a while, I've attempted to recompile different source codes without the fgets() function, and they've compiled correctly. I'm guessing that the fgets() function is what was causing my syntax error due to undefined behavior provoked by the function, as pointed out by alk, since any code without it is now compiling without error, so I'm considering this question answered.

在让我的计算机休息一会儿之后,我尝试重新编译不同的源代码,而没有fgets()函数,它们已经正确编译了。我猜测fgets()函数是由于函数所引发的未定义的行为(如alk所指出的)导致的语法错误,因为没有它的任何代码现在都没有错误地编译,所以我正在考虑这个问题的答案。

#4


0  

The problem is apparently the lack of space after the hash (#) and before include.
After I added the space, it worked like a charm.

问题显然是在散列(#)和之前包含的空间不足。在我添加了空间之后,它就像一种魔力。

(I know the thread is a bit old, but Google brought me here because I had the same problem and was trying to figure out what was wrong, so maybe this will help someone else.)

(我知道这条线有点旧,但是谷歌把我带到了这里,因为我遇到了同样的问题,想弄清楚到底出了什么问题,所以也许这能帮到别人。)

#1


0  

Your line

你的线

int main (int argc, char *argv)

is wrong. It must be

是错误的。它必须是

int main (int argc, char *argv[])

OR

int main (int argc, char **argv) //less common, though

And also your line

你也行

char string = "JD";

should be

应该是

const char *string = "JD";

Also, I don't get

另外,我不明白

scanf("%s", &argv);

why would you read something INTO argv?

你为什么要读到argv?

#2


0  

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 1024  // a normal buffer size for fgets
int main (int argc, char *argv[])
{
    FILE *fp;
    //char string = "JD";
    //char data;
    char buffer[MAXLINE];
    if(argc != 2)
    {printf("usage : ./a.out <filename>\n"); return -1;}

    if((fp = fopen(argv[1],"r+")) == NULL)
    {printf("open file %s error\n",argv[1]); return -1;}

   /*
    while((data = getchar()) != EOF)
    {
        fgets(data, sizeof(data), fp);
        data = fputs(string, fp);
    }*/      //I don't understand this part .


    /* while we're not at end of file */
    while (fgets(buffer,MAXLINE , fp) != NULL)
    {
        printf("%s\n",buffer); /* print the string */
    }

    fclose(fp); /* close the file */
    return 0; /* success */
}

#3


0  

After letting my computer rest for a while, I've attempted to recompile different source codes without the fgets() function, and they've compiled correctly. I'm guessing that the fgets() function is what was causing my syntax error due to undefined behavior provoked by the function, as pointed out by alk, since any code without it is now compiling without error, so I'm considering this question answered.

在让我的计算机休息一会儿之后,我尝试重新编译不同的源代码,而没有fgets()函数,它们已经正确编译了。我猜测fgets()函数是由于函数所引发的未定义的行为(如alk所指出的)导致的语法错误,因为没有它的任何代码现在都没有错误地编译,所以我正在考虑这个问题的答案。

#4


0  

The problem is apparently the lack of space after the hash (#) and before include.
After I added the space, it worked like a charm.

问题显然是在散列(#)和之前包含的空间不足。在我添加了空间之后,它就像一种魔力。

(I know the thread is a bit old, but Google brought me here because I had the same problem and was trying to figure out what was wrong, so maybe this will help someone else.)

(我知道这条线有点旧,但是谷歌把我带到了这里,因为我遇到了同样的问题,想弄清楚到底出了什么问题,所以也许这能帮到别人。)