You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iostream/copy_file_with_chars.cpp

16 lines
502 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream ifile("hello.txt");
std::ofstream ofile("hello_copy.txt");
// 忽略文件对象判断的过程
ifile.unsetf(std::ios::skipws); // 设置不跳过空白符的flag
char ch{};
while (ifile >> ch) // 如果没有上方语句可以ifile.get(ch)
ofile << ch;
ifile.close(); // 如没有这两个函数析构函数也可关闭,但建议显式书写
ofile.close();
return 0;
}