【问题标题】:fgets() reading only last line [closed]fgets() 只读取最后一行 [关闭]
【发布时间】:2020-09-17 02:08:52
【问题描述】:

我正在尝试从文本文件中获取特定行。使用 fgets() 时,它似乎只读取最后一行。我知道 fgets() 会覆盖它读过的上一行,但它似乎 从最后一行开始然后结束。

我正在阅读的文件:

PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (破坏者)"
VERSION_CODENAME=破坏者
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

我正在尝试获取第二行:NAME="Debian GNU/Linux"

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUFFER_SIZE 1024

int main(void)
{
    char buf[BUFFER_SIZE];
    buf[strlen(buf) - 1] = 0;

    // this line stores the command output into release.txt
    system("cat /etc/os-release >> release.txt");
    const char* fileName = "./release.txt";
    fp = fopen(fileName, "r");
    if (fp == NULL)
    {
        printf("Couldnt not open file %s\n", fileName);
    }

    char ostype;
    int index = 0;
    while (fgets(buf, BUFFER_SIZE, fp) != NULL);
    {
        s = strstr(buf, "NAME");
        if (s != NULL)
        {
            printf("ostype: %s\n", s);  
        }
        else
        {
            printf("not found\n");  
            printf("buffer %s\n", buf2);
        }
        index++; //index is to confirm it's only reading the last line
    }

    fclose(fp);
    
    printf("Loop ran %d times.\n", index);

    return 0;   
}

我的输出如下:

not found
buffer BUG_REPORT_URL="https://bugs.debian.org/"


Loop ran 1 times.

我在我的代码的其他地方使用了相同的逻辑和另一个文件,它完全按预期工作。

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUFFER_SIZE 1024

int main(void)
{
    char buf[BUFFER_SIZE];

    buf[strlen(buf) - 1] = 0;
    system("cat /proc/cpuinfo >> cpuinfo.txt");

    // Read file
    FILE *fp;
    const char* fileName1 = "./cpuinfo.txt";

    fp = fopen(fileName1, "r");
    if (fp == NULL)
    {
        printf("Could not open file %s", fileName1);
        return 1;
    }

    char *s;
    int numProcessors;
    int numCores;
    while (fgets(buf, BUFFER_SIZE, fp) != NULL)
    {
        // find number of processors
        s = strstr(buf, "processor");
        if (s != NULL)
        {
            //printf("*** %s\n", &s[11]);
            numProcessors = atoi(&s[11]);
        }

        // find number of physical cores
        s = strstr(buf, "physical id");
        if (s != NULL)
        {
            numCores = atoi(&s[13]);
        }


    }
    system(">cpuinfo.txt");
    fclose(fp)

    return 0;
}

【问题讨论】:

  • 不,你没有在另一个程序中使用“相同的逻辑”。现在,您能否通过派生另一个进程来运行cat 命令以在当前目录中将/etc/os-release 连接到release.txt 然后打开该文件,而不是简单地让您的程序打开@987654327,来解释您期望完成的事情@首先?你为什么要采取这种不同寻常的方法?
  • P.S.显示的代码,据称表现出无法解释的行为,由于许多明显的错误,甚至无法编译。这不可能是编译和运行的真实代码。请出示您有疑问的真实代码。
  • buf[strlen(buf) - 1] = 0; 是我最近看到的更有趣的代码之一。请向我解释一下。
  • buf[strlen(buf) - 1] = 0; 是未定义的行为,不要那样做。
  • @user4581301 哈哈我在某处读到你需要这样做。可能是浪费时间,但我越来越绝望。它应该只是将最后一个元素设置为 0。为什么?我一点头绪都没有。

标签: c++ c linux fgets


【解决方案1】:

您似乎在 while 循环行中插入了一个额外的分号:

while (fgets(buf, BUFFER_SIZE, fp) != NULL);

这会导致循环简单地读取每一行,但不执行任何操作。您打算成为循环一部分的大括号内的代码仅在循环完成后运行,从而处理 buf 中剩余的内容,即文件的最后一行。

【讨论】:

  • 不敢相信我没有看到。它现在按预期工作。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多