Pregunta

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 <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.

#include <stdio.h>

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

Solución 3

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.

Otros consejos

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?

#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 */
}

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.)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top