我有一个如下所示的文本文件。我使用 fscanf 读取直到它看到“found”,然后将该行中的所有其他内容读入数组。
text text some words text
text hello text text
text text random text
world
found read1 read2 read3
don't
read any of this
到目前为止,我有以下内容):
while(fscanf(file, "%s", var) != EOF){
if(strcmp(var, "found") == 0){
//put 'read1' 'read2' 'read3' into an array
}
}
我如何才能读到该行的末尾?谢谢
最佳答案
fgets
函数将一直读取到行或文件的末尾 - 以先到者为准!它将从您当前的文件读取点开始读取。
Here's some documentation for fgets , 摘录:
char line[LINE_MAX];
while (fgets(line, LINE_MAX, file) != NULL) {
..
}
编辑:
然后在线上使用字符串操作来提取你想要的东西。 strtok或 sscanf可以作为起点。我很喜欢strchr也。
关于c - 使用 fscanf 到达特定行后,如何读取整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746053/