如何复制文本文件中的C或C ++? [英] How to copy text file in C or C++?
本文介绍了如何复制文本文件中的C或C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当尝试一个文本文件复制到另一个文件B,有可能有几种方法:
1)逐字节
2)一字一句
3)一行行
When trying to copy a text file A to another file B, there may have several methods: 1) byte by byte 2) word by word 3) line by line
哪一个更有效?
推荐答案
使用缓冲区:
#include <fstream>
int main()
{
std::ifstream inFile("In.txt");
std::ofstream outFile("Out.txt");
outFile << inFile.rdbuf();
}
C ++的fstreams内部缓冲。他们使用一个有效的缓冲区大小(不管什么人说流:-)效率。所以,只是一个流缓冲区复制到流哎preSTO内部魔术会做一个流的有效复制到其他。
The C++ fstreams are buffered internally. They use an efficient buffer size (despite what people say about the efficiency of stream :-). So just copy one stream buffer to a stream and hey presto the internal magic will do an efficient copy of one stream to the other.
但学习做成炭使用std ::复制(CHAR)是这么多的乐趣。
But learning to do it char by char using std::copy() is so much more fun.
这篇关于如何复制文本文件中的C或C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文