【问题标题】:How can i read only certain line from txt file in c?我如何才能从 c 中的 txt 文件中只读取某些行?
【发布时间】:2017-10-26 00:58:27
【问题描述】:

我有这个文件,我想在变量中只保存不以 //:

开头的行
// Dimensoes da janela em termo de bolhas (largura e altura)
60 40
// Dimensão da bolha em pixeis (raio)
5
// dl – distancia para avaliação da colisão (percentagem do diametro)
0.95
// Numero de linhas inicias com bolhas
6
// Geração de uma nova linha de bolhas ao fim de N jogadas
10

我不确定我该怎么做。我试过 fscanf 和 fgets 但我做得不对 提前致谢

【问题讨论】:

  • 阅读每一行,如果不是以//开头,请保留。
  • 我应该使用 fgets 并保存到字符串中吗?
  • 是的,使用fgets()

标签: c file scanf line fgets


【解决方案1】:

你可以试试这样的:

FILE * inputFile; 
char str[256];
inputFile = fopen("fileName.txt", "r");
while(fgets(str, 256, inputFile) != NULL) {
    if (str[0] == '/' && str[1] == '/') {
        // Save string here
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多