c - 使用 fscanf 到达特定行后,如何读取整行?

标签 c

我有一个如下所示的文本文件。我使用 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) {
..
}

编辑:
然后在线上使用字符串操作来提取你想要的东西。 strtoksscanf可以作为起点。我很喜欢strchr也。

关于c - 使用 fscanf 到达特定行后,如何读取整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746053/

相关文章:

c - 带字符串的 Switch 语句?

c - 在C中查找BST中奇数/偶数/负数的数量

将整数值转换为十六进制字符串

c - NGINX 中的模块链接

c - 这个输出是怎么来的?

c# - Visual Studio 2019 : Insert multiple C++ style single-line comments

c - 需要帮助了解缓冲区溢出及其利用

c - 如何用C语言打印2张表格?

c - 定义对象宏

c - scanf 函数不适用于字符