From 72eead16ad2f1e8b9b0f24fdd8bf2ed5a74a8c28 Mon Sep 17 00:00:00 2001 From: p68710245 Date: Sun, 28 Apr 2024 19:33:46 +0800 Subject: [PATCH] Add copy_file_with_chars --- copy_file_with_chars | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 copy_file_with_chars diff --git a/copy_file_with_chars b/copy_file_with_chars new file mode 100644 index 0000000..8f36056 --- /dev/null +++ b/copy_file_with_chars @@ -0,0 +1,16 @@ +#include +#include +#include +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; +} \ No newline at end of file