错误c2059:语法错误:从文件读取时c中的常量

时间:2014-12-11 10:42:47

标签: c printf fopen

我想从文本文件中读取并打印前两行。 问题是,我收到错误:错误c2059:语法错误:常量,它指向我的文本文件中的第一行。 有什么想法吗?

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

代码:

#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;
}

1 个答案:

答案 0 :(得分:1)

Visual Studio将编译项目中的每个文件。这包括file.txt,如果您已将其作为文件添加到项目中。

要阻止Visual Studio编译此内容,您需要告诉Visual Studio它是一个“内容”文件。在 Build Action Property 上查看 File Properties

  

内容 - 文件未编译,但包含在内容输出组中。例如,此设置是.htm或其他类型的Web文件的默认值。