通过使用get()复制文件来获取垃圾值
Getting a junk value from copying a file using get()
所以我编写了一些代码,这些代码将通过打开要复制的文件,然后使用char使用get复制char来复制文件。代码是:
int main(int argc, char* argv[]) {
if (argc != 3) {
cout << "ERROR: Invalid number of arguments.n";
return 1;
}
ifstream inputFile(argv[1]);
ofstream outputFile(argv[2]);
char c;
if (inputFile.is_open() && outputFile.is_open()) {
while (!(inputFile.eof())) {
c = inputFile.get();
outputFile << c;
}
}
else {
cout << "Unable to open file(s).n";
return 1;
}
inputFile.close();
outputFile.close();
return 0;
}
它完美地复制了文本,除了总有一个垃圾字符。(注意:我知道有更好的方法复制文件,但是我必须使用get())。
循环应该像这样:
for (int c; (c = inputFile.get()) != EOF; )
{
if (!outputFile.put(c))
{
// fatal: write error, die
}
}
相关文章:
- C++ - 忽略并从其他文件获取数据
- 为什么从文件获取图标时应用程序有时会崩溃?
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- Qt .pro 文件获取 Qtkit 名称
- 提升属性树:如何使用xml文件获取子树的子级
- 从C 中的二进制文件获取整数值时
- 如何在Windows上绕过主机文件获取真实的IP地址
- 如何使用 CPP/C# 从 VHDX 文件获取文件(或文件系统)
- 从读取到对象指针数组的文件获取段错误
- 是否可以从IOS项目中的cpp文件获取文件路径
- 将数据从头文件获取到源文件中的非成员函数中
- 从两个不同的文件获取输入时,输入会发生变化
- 从文件 C++ 获取整数
- 通过在 C++ 中使用发布模式二进制文件获取崩溃原因
- C++ 将值从文件获取到数组中
- 使用HTTP上载文件.获取错误:-HttpSendReuest 12005
- 正在从外部文件获取输入
- 读/写同一个文件(获取gmon.out)
- 从二进制文件获取输入时发生访问冲突错误
- 从特定文件获取函数指针