0

I want to read and print the first two lines from a text file. Problem is, I get the error: error c2059: syntax error: constant and it points to the first line in my text file. Any ideas?

The file.txt:

5
5
3
1 1 1 0 0
0 1 0 0 1
0 1 0 1 0
1 0 1 0 1
1 1 0 1 1

The code:

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

int main() {
    FILE *fp;
    int line, col, gen;
    fp = fopen("file.txt", "rt");
    fscanf(fp, "%d\n,%d\n", &line, &col);
    printf("line: %d,  col: %d\n", line, col);
    fclose(fp);
    return 0;
}
Avital
  • 31
  • 1
  • 7
  • 7
    Are you trying to **compile** the `file.txt`? Please tell us how you are compiling the files. – bzeaman Dec 11 '14 at 10:49
  • ctrl F5. Am I doing it wrong? I have visual studio for barely a day... – Avital Dec 11 '14 at 10:52
  • But are you trying to compile `file.txt`? VS is probably trying to compile all active files, among which `file.txt`, which it shouldn't. –  Dec 11 '14 at 10:54
  • Something is going wrong indeed. Apparently Visual Studio is compiling your text file. Text files are not meant to be compiled, they are not containing code. – bzeaman Dec 11 '14 at 10:54
  • Then how do I change that? – Avital Dec 11 '14 at 10:55
  • 2
    Take a look [at this answer](http://stackoverflow.com/a/1447110/2703418), I think it might help you. Apparently you need to tell Visual Studio that it's a 'Content' file. – bzeaman Dec 11 '14 at 10:55
  • It works! I changed the file's properties to content and text file. – Avital Dec 11 '14 at 11:00
  • Great, I've added the solution as an answer which might be helpful for people in the future. You may accept it as an answer. I don't have much experience with Visual Studio, so it might not exactly be accurate. – bzeaman Dec 11 '14 at 11:03

1 Answers1

1

Visual Studio will compile every file in your project. This includes file.txt, if you have added it as a file to your project.

To prevent Visual Studio from compiling this, you need to tell Visual Studio it's a 'Content' file. Take a look at File Properties at Build Action Property.

Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.

bzeaman
  • 1,128
  • 11
  • 28