1

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;
}
Josh
  • 11
  • 1
  • 1
  • 5
  • This `fgets(data, sizeof(string), fp)` provokes undeinfed bahaviour as reading to an invalid address, namely the value of `data`. – alk Aug 16 '13 at 09:47
  • Oddly, that worked when I used `fp = fopen("sample.txt", "r")`. But now, for some reason, that's not even working anymore. – Josh Aug 16 '13 at 10:19
  • What command line you're using to compile? Does your compiler compile anything right? Did you tried another compiler (like DMC)? It seems problem is in your compiler. – Vovanium Aug 16 '13 at 11:29
  • @Josh: you learned an import aspect of "Undefined Behaviour" though: Anything can happen! :-) – alk Aug 16 '13 at 11:59
  • Please quote the **exac**t error message, **including** file name and line number. – alk Aug 16 '13 at 12:02
  • @alk The exact error message is `helloworld.c: line 3: syntax error before unexpected token '('` followed by `helloworld.c: line 3: 'int main(void)'` or anything else I put in there instead of `(void)`. – Josh Aug 16 '13 at 13:23
  • @Vovanium I'm using the command line packaged with MinGW (msys.bat). It compiled a previous version of the file I/O code above, in which I was specifying what file was to be read. It also worked, once, using `void main(void)` on a the above mentioned "hello world" code, which I received the same error message on shortly after (even `int main(void){printf("hello"); return 0;}` throws the same error. – Josh Aug 16 '13 at 13:29
  • I can see only two possibilities here : 1) there is a problem in your header so try comment out the `include` and `printf` statements to see if it works out ....OR... 2) try to remove `void` from `main` and see if it works ( I know using void is standard ) but for now just try it and then come back to tell us what happened ? – 0decimal0 Aug 16 '13 at 14:15
  • Which compiler are you using? Does `int main(void){return 0;}` (without any includes) compile? – alk Aug 16 '13 at 14:15
  • `int main(void{return 0;}` compiles without includes. I retried `void main(void){printf("hello");}` including ``, and that compiled, so I switched it back to `int main(void)`, and it compiled again. I'm basically at a loss now. I'm not getting the error anymore, at least not that one. I'm still confused as to what may have caused it, my guess is the "undefined behavior", as alk put it. I'm using MinGW. – Josh Aug 16 '13 at 19:48
  • Most likely the space you had between `int` and `main` was not actually a space (perhaps a nbsp, ascii value 0xC0). Rewriting the line fixed it. – Per Johansson Jun 19 '14 at 22:16
  • Oh sorry this was ages ago. Not sure how it showed up for me now. – Per Johansson Jun 19 '14 at 22:17

4 Answers4

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?

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • I prefer the third option for `main`. It explicitly says what `argv` is, while the second tries to hide it by making it look like it's an array. – chris Aug 16 '13 at 09:34
  • @bash.d I've edited it to `int main(int argc, char **argv)`, as well as `const char *string = "JD"`. I'm reading the filename into `argv`. – Josh Aug 16 '13 at 09:51
  • So, do you still get an error? `argv` is not meant for values to be read into it... – bash.d Aug 16 '13 at 09:53
  • Yep, I still get an error. I've tried switching to `scanf("%s", &filename)`, declaring `char filename[]` first, but still getting that error. – Josh Aug 16 '13 at 09:58
  • You still haven't updated to `int main (int argc, char **argv) ` – bash.d Aug 16 '13 at 10:22
  • I noticed that, and just updated it to `int main(void)` since I switched back to reading from a specified file. I'm still getting the error message though. It worked with a specified file earlier, minus the code that I've commented out, but now it keeps throwing that same error message at me even though I've reverted it back to the same code it was when it compiled without issue. – Josh Aug 16 '13 at 10:26
  • I went even further, I wrote a "hello world" app and I'm still getting that same error message. I can't figure it out. – Josh Aug 16 '13 at 10:37
  • How old is your compiler? – bash.d Aug 16 '13 at 10:58
  • The most recent version of MinGW, I believe. The release date for my version was 4/26/2012. – Josh Aug 16 '13 at 11:16
  • Do you know how to preprocess using MinGW? If that works, try to compile only, but not link the file... – bash.d Aug 16 '13 at 11:21
  • I've tried `-E`, `-S` and `-c`, but I still get that error message. – Josh Aug 16 '13 at 11:30
  • Okay, that worked for the "hello world" app, but my original code is still throwing that error after changing to `void main(void)` and removing the `return`s. – Josh Aug 16 '13 at 11:49
  • Have you tried to shut down your computer and restart? If that doesn't help, look for an update or get `gcc` or VS2010 Express... – bash.d Aug 16 '13 at 11:50
  • The most recent version of MinGW is from 11/5/2012, FYI – bash.d Aug 16 '13 at 11:51
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 */
}
Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
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.

Josh
  • 11
  • 1
  • 1
  • 5
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.)

kmajaa
  • 1