C++ C+中的Fgets+;重复最后一行

C++ C+中的Fgets+;重复最后一行,c++,file,fgets,C++,File,Fgets,我有类似(来自)的程序 排成一排 A B C D E 程序的输出是 A B C D E E 它重复文件的最后一行两次。我在其他程序中也有这个问题。问题是最后一行fgets将失败。但是,直到下一个循环,您才检查feof,所以您仍然调用fputs,它将打印缓冲区的内容,即前一行 试试这个: FILE* soubor; char buffer[100]; soubor = fopen("file","r"); string outp = ""; while (true) { fgets(buf

我有类似(来自)的程序

排成一排

A
B
C
D
E
程序的输出是

A
B
C
D
E
E

它重复文件的最后一行两次。我在其他程序中也有这个问题。

问题是最后一行fgets将失败。但是,直到下一个循环,您才检查feof,所以您仍然调用fputs,它将打印缓冲区的内容,即前一行

试试这个:

FILE* soubor;
char buffer[100];
soubor = fopen("file","r");
string outp = "";
while (true)
{
  fgets(buffer,100,soubor);
  if (feof(soubor))
    break;
  fputs (buffer , stdout);
}
fclose(soubor);

问题是,对于最后一行,fgets将失败。但是,直到下一个循环,您才检查feof,所以您仍然调用fputs,它将打印缓冲区的内容,即前一行

试试这个:

FILE* soubor;
char buffer[100];
soubor = fopen("file","r");
string outp = "";
while (true)
{
  fgets(buffer,100,soubor);
  if (feof(soubor))
    break;
  fputs (buffer , stdout);
}
fclose(soubor);
使用循环作为从文件读取的条件几乎总是会导致问题。标准方式如下所示:

while (fgets(buffer, 100, infile))
    fputs(buffer, stdout);
使用循环作为从文件读取的条件几乎总是会导致问题。标准方式如下所示:

while (fgets(buffer, 100, infile))
    fputs(buffer, stdout);

我喜欢本·拉塞尔的回答。这是我的版本,以避免重复c代码中的最后一行。它可以工作,但我不明白为什么,因为条件
if(fgets!=NULL)
应该可以工作

int main ()
{
    FILE* pFile;
    char name[41] = "fileText04.txt";
    char text[81];
    int i;

    pFile = fopen("fileText04.txt", "wt");
    if (pFile == NULL)
    {
        printf("Error creating file \n");
        exit(1);
    }
    else
    {
        for (i=0; i<5; i++)
        {
            printf("Write a text: \n");
            fgets(text, 81, stdin);
            fputs(text, pFile);
        }
    }
    fclose (pFile);
    pFile = fopen(name, "rt");
    if (pFile == NULL)
    {
        printf("File not found. \n");
        exit(2);
    }
    while (! feof(pFile))
    {
        fgets(text, 80, pFile);
        if (feof(pFile))   // This condition is needed to avoid repeating last line.
            break;         // This condition is needed to avoid repeating last line.
        if (fgets != NULL)
            fputs(text, stdout);
    }
    fclose (pFile);
    return 0;
}
int main()
{
文件*pFile;
字符名[41]=“fileText04.txt”;
字符文本[81];
int i;
pFile=fopen(“fileText04.txt”,“wt”);
if(pFile==NULL)
{
printf(“创建文件时出错”);
出口(1);
}
其他的
{

对于(i=0;i我喜欢Ben Russels答案。这是我的版本,以避免重复c代码中的最后一行。它可以工作,但我不理解为什么,因为条件
if(fgets!=NULL)
应该可以工作

int main ()
{
    FILE* pFile;
    char name[41] = "fileText04.txt";
    char text[81];
    int i;

    pFile = fopen("fileText04.txt", "wt");
    if (pFile == NULL)
    {
        printf("Error creating file \n");
        exit(1);
    }
    else
    {
        for (i=0; i<5; i++)
        {
            printf("Write a text: \n");
            fgets(text, 81, stdin);
            fputs(text, pFile);
        }
    }
    fclose (pFile);
    pFile = fopen(name, "rt");
    if (pFile == NULL)
    {
        printf("File not found. \n");
        exit(2);
    }
    while (! feof(pFile))
    {
        fgets(text, 80, pFile);
        if (feof(pFile))   // This condition is needed to avoid repeating last line.
            break;         // This condition is needed to avoid repeating last line.
        if (fgets != NULL)
            fputs(text, stdout);
    }
    fclose (pFile);
    return 0;
}
int main()
{
文件*pFile;
字符名[41]=“fileText04.txt”;
字符文本[81];
int i;
pFile=fopen(“fileText04.txt”,“wt”);
if(pFile==NULL)
{
printf(“创建文件时出错”);
出口(1);
}
其他的
{
对于(i=0;i,当复制文件时,feof(inputfile\u指针)不是检查终止的正确方法的原因是,它在以下两种情况下都不起作用:

  • 文件结尾没有换行符
  • 文件以换行符结尾
  • 证明:

    • 假设在
      fgets()
      之后,但在
      fputs()
      之前检查了
      feof
      。那么它不适用于案例1。如上所述,因为在EOF之前读取的任何字符
      fgets()
      都不会被
      fputs()
      使用
    • 假设在
      fputs()
      之后,但在
      fgets()
      之前选中了
      feof
      。那么它对案例2不起作用。如上所述,当
      fgets()
      最后遇到EOF时,它不会用任何新的内容覆盖缓冲区字符串,并且是
      fputs()
      允许再运行一次,它将在输出文件中放入与上一次迭代相同的缓冲字符串内容;因此重复输出文件中的最后一行
    复制文件时,feof(inputfile\u指针)不是检查终止的正确方法的原因是,它在以下两种情况下都不起作用:

  • 文件结尾没有换行符
  • 文件以换行符结尾
  • 证明:

    • 假设在
      fgets()
      之后,但在
      fputs()
      之前检查了
      feof
      。那么它不适用于案例1。如上所述,因为在EOF之前读取的任何字符
      fgets()
      都不会被
      fputs()
      使用
    • 假设在
      fputs()
      之后,但在
      fgets()
      之前选中了
      feof
      。那么它对案例2不起作用。如上所述,当
      fgets()
      最后遇到EOF时,它不会用任何新的内容覆盖缓冲区字符串,并且是
      fputs()
      允许再运行一次,它将在输出文件中放入与上一次迭代相同的缓冲字符串内容;因此重复输出文件中的最后一行
    标准模式(在所有porocedural语言中)是将get语句作为条件放入循环。如果失败,则永远不会输入循环。标准模式(在所有porocedural语言中)是将get语句作为条件放入循环。如果失败,则永远不会输入循环。