包含 FGETC 和 FEOF 的文件预计会发生什么?

whats is expected to happen to a file with fgetc and feof?

本文关键字:什么 FGETC FEOF 文件 包含      更新时间:2023-10-16

im 试图学习 c/c++,我正在创建一个"追加"函数,它会将"inFile"文本附加到"outFile"中,如果它不存在,则创建"outFile"。

int AppendFile(const char * inFIle, const char * outFile)
{
FILE * ptrInputFile = fopen(inFIle, "rb");                      /*Open first file*/
if (ptrInputFile == NULL)
{
fprintf(stderr, "Error opening "%s"", inFIle);            /*Output to cerr*/
return -1;
}
FILE * ptrOutputFile = fopen(outFile, "ab");                    /*Open second file*/
if (ptrOutputFile == NULL)
{
fclose(ptrInputFile);                                       /*Close inFile that was openned in lines above*/
fprintf(stderr, "Error opening "%s"", outFile);           /*Output to cerr*/
return -1;
}
/*read char by char and append.*/
while (1)
{
char tempChar = fgetc(ptrInputFile);                        /*Read char in stream*/
if (feof(ptrInputFile))                                     /*Check if EOL if so, break*/
{
break;
}
fprintf(ptrOutputFile, "%c", tempChar);                     /*Send character to output stream*/
}
/*Close files.*/
fclose(ptrInputFile);
fclose(ptrOutputFile);
return 0;                                                      /*Return successfully*/
}

上面的代码有效,但我的第一次迭代是:

/*read char by char and append.*/
while (1)
{
if (feof(ptrInputFile))                                     /*Check if EOL if so, break*/
{
break;
}
char tempChar = fgetc(ptrInputFile);                        /*Read char in stream*/
fprintf(ptrOutputFile, "%c", tempChar);                     /*Send character to output stream*/
}

但是将 fget 放在那里会在两个文件之间产生0xFF,怎么可能呢? 据我了解,"feof"嗅探 strrean 中的当前 caracter 以检查 EOF,"fget"返回字符并推进指针,对吧? 所以我的逻辑是,首先检查 for 和 EOF,这样你就不会将 EOF 写入流, 但结果是溪流中的0xFF,怎么可能?

按照我的逻辑,以下代码就可以了:

while (1)
{
char tempChar = fgetc(ptrInputFile);                        /*Read char in stream*/
if (feof(ptrInputFile))                                     /*Check if EOL if so, break*/
{
break;
}
fprintf(ptrOutputFile, "%c", tempChar);                     /*Send character to output stream*/
}

获取字符,然后嗅闻然后写下它,所以在最后一个字符处,它会读取它并嗅探 EOL 并退出,这样最后一个字符就会丢失,我怎么可能没有丢失任何字符?

谢谢你的帮助。

附言。我知道我可以将 fstream 类用于 c++,但我所学的课程是关于 fopen、fclose 和 fprint,所以我不得不使用这些类。

编辑: 对我来说,算法的工作方式是违反直觉的,所以也许我没有看到一些东西,例如,有代码:

while (1)
{
if (feof(ptrInputFile))
{
break;
}
char tempChar = fgetc(ptrInputFile);
fprintf(ptrOutputFile, "%c", tempChar);
} 

该文件包含"H","EOF","ptrInputFile"将指向"H",而迭代如下: 1. H是EOF?不,继续,将"H"复制到 tempChar 和前进指针(fgetc 这样做),将"H"写入 ptrOutputFile。 2. "ptrInputFile"现在指向"EOF","EOF"是EOF,是的中断。

出于某种原因,这段代码正在给我写一个额外的字符,一个0xFF。

工作正常的代码是:

while (1)
{
char tempChar = fgetc(ptrInputFile);
if (feof(ptrInputFile))
{
break;
}
fprintf(ptrOutputFile, "%c", tempChar);
} 

像上面的例子,文件包含'H','EOF',ptrInputFile指向'H',迭代如下(至少我所理解的): 1. 将"H"复制到 tempchar 并前进指针 1,现在 ptrInputFile 指向 EOF,ptrInputFile 是 EOF 吗?是的,好的休息。

所以对我来说,它不应该写任何东西,但它确实写了,我的问题是 feof 和 fgetc 究竟是如何工作的。

这是为什么"while ( !feof (file))"总是错误的变体?EOF的测试是在可能找到EOF的读取之前完成的,因此使用eof。

while后立即退出循环的 if 语句与将if的条件放在while循环中没有什么不同。

while (1)
{
if (feof(ptrInputFile)) //Check if we SAW eof. This checks before the read
{
break;
}
char tempChar = fgetc(ptrInputFile); // read may well be EOF, but you don't check.
fprintf(ptrOutputFile, "%c", tempChar); // and wind out printing the EOF. Oops
}

与 相同

while (feof(ptrInputFile)) //Check if we SAW eof. This checks before the read
{
char tempChar = fgetc(ptrInputFile); // read may well be EOF, but you don't check.
fprintf(ptrOutputFile, "%c", tempChar); // and wind out printing the EOF. Oops
}

你想要的看起来更像

int tempChar; // note int, not char. fgetc returns an int to make sure EOF is out
// of char range
while ((tempChar = fgetc(ptrInputFile)) != EOF) //read then test if we read  EOF
{
fprintf(ptrOutputFile, "%c", (char)tempChar); // print known-good character
}