包含 FGETC 和 FEOF 的文件预计会发生什么?
whats is expected to happen to a file with fgetc and feof?
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
}
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 包含 FGETC 和 FEOF 的文件预计会发生什么?