Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c中只读取txt文件中的某一行?_C_File_Scanf_Line_Fgets - Fatal编程技术网

如何在c中只读取txt文件中的某一行?

如何在c中只读取txt文件中的某一行?,c,file,scanf,line,fgets,C,File,Scanf,Line,Fgets,我有这个文件,我只想在变量中保存不以//开头的行: // 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 /

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

// 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,但我做得不对
提前感谢

您可以尝试以下方法:

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

阅读每一行,如果不是以//开头,则保留它。我是否应该使用fgets并保存为字符串?是的,使用fgets。