[请不要评论使用 Turbo C++。我知道它已经过时了,但我们只被这样教导。] 这里有一些类似的错误 Why do I get a 'ÿ' char after every include that is extracted by my parser? - C但我无法将它与我的代码联系起来 - 我是新手。
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
ifstream read_file;
read_file.open("Employee.txt");
ofstream write_file;
write_file.open("Another.txt");
while(!read_file.eof())
{
/*Also when I use, write<<read_file.get(ch) in this block instead of the two statements below, it writes some kind of address in the file. Please tell me about that too why it happens. */
read_file.get(ch);
write_file<<ch;
}
read_file.close();
write_file.close();
getch();
}
我面临的问题是它在“另一个”文件的末尾附加了 ÿ 字符。
例如:“员工”中的文字是, 编号:1 姓名:abc 那么它复制到“另一个”的文本是: 编号:1 姓名:abcÿ
最佳答案
一旦您读完最后一个字符,eof()
检查将不会返回 true;在您尝试读完之后之前,它一直为 false。因此,与其在 while 循环条件中检查 eof,不如在阅读后(但在写入之前)立即检查它,然后中断。
(顺便解释一下:ÿ 是值 0xFF 的 ANSI 字符表示,也就是说,-1。这就是 get()
返回信号 EOF 的内容。因此,如果你想要的,而不是检查 eof()
,你可以看到 char 是否等于 -1。)
关于c++ - 文件处理,复制内容时插入 ÿ 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19604615/