C++ fgets()仅读取最后一行

C++ fgets()仅读取最后一行,c++,c,linux,fgets,C++,C,Linux,Fgets,我试图从文本文件中提取一行特定的内容。使用fgets()时,它似乎只读取最后一行。我知道fgets()覆盖了它读过的前一行,但它似乎从最后一行开始,然后结束 我正在尝试读取的文件: PRETTY_NAME=“Debian GNU/Linux 10(buster)” NAME=“Debian GNU/Linux” VERSION_ID=“10” VERSION=“10(buster)” VERSION\u CODENAME=buster ID=debian 主页地址=”https://www.de

我试图从文本文件中提取一行特定的内容。使用fgets()时,它似乎只读取最后一行。我知道fgets()覆盖了它读过的前一行,但它似乎从最后一行开始,然后结束

我正在尝试读取的文件:

PRETTY_NAME=“Debian GNU/Linux 10(buster)”
NAME=“Debian GNU/Linux”
VERSION_ID=“10”
VERSION=“10(buster)”
VERSION\u CODENAME=buster
ID=debian
主页地址=”https://www.debian.org/“
支持URL=”https://www.debian.org/support“
错误报告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;
    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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义缓冲区大小1024
内部主(空)
{
char buf[缓冲区大小];
buf[strlen(buf)-1]=0;
系统(“cat/proc/cpuinfo>>cpuinfo.txt”);
//读取文件
文件*fp;
const char*fileName1=“./cpuinfo.txt”;
fp=fopen(文件名1,“r”);
如果(fp==NULL)
{
printf(“无法打开文件%s”,文件名1);
返回1;
}
char*s;
整数处理器;
国际货币联盟;
while(fgets(buf,BUFFER_SIZE,fp)!=NULL)
{
//查找处理器的数量
s=strstr(buf,处理器);
如果(s!=NULL)
{
//printf(“***%s\n”和&s[11]);
numProcessors=atoi(&s[11]);
}
//查找物理核心数
s=strstr(buf,物理id);
如果(s!=NULL)
{
numCores=atoi(&s[13]);
}
}
系统(“>cpuinfo.txt”);
fclose(fp)
返回0;
}

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

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

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

否,您没有在其他程序中使用“相同的逻辑”。现在,您能否解释一下,通过分叉另一个进程来运行
cat
命令,将当前目录中的
/etc/os release
连接到
release.txt
,然后打开该文件,而不是简单地让程序首先打开
/etc/os release
?为什么要采用这种不寻常的方法呢?顺便说一句,所显示的代码,据称表现出无法解释的行为,甚至可能无法编译,因为有许多明显的错误。这不可能是编译和运行的真正代码。请显示您有疑问的真实代码。
buf[strlen(buf)-1]=0
是我最近看到的比较有趣的代码之一。请给我解释一下。
buf[strlen(buf)-1]=0是未定义的行为,不要这样做。@user4581301哈哈,我在某个地方读到你需要这样做。可能是浪费时间,但我已经绝望了。它应该只将最后一个元素设置为0。为什么?我一点也不知道。真不敢相信我没看到。它现在的工作情况和预期的一样。谢谢你的帮助!
#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;
}
while (fgets(buf, BUFFER_SIZE, fp) != NULL);