题目:给定任意一个文件名,读取该文件,并将文件中的数据复制一份,以追加的方式写入到该文件中。
实现:
/**
* 读取当前文件追加到当前文件中
*/
void test_readappendfile(){
ifstream infile("test.txt", ifstream::in|ifstream::binary);
ofstream outfile("test.txt", ifstream::app|ifstream::binary);
infile.seekg(0, ifstream::end);
ifstream::streampos end = infile.tellg();
infile.seekg(0, ifstream::beg);
char b[512] = {0};
int c = 0;
for(int i = 0; i<end; i+=c){
infile.read(b, sizeof(b)/sizeof(char));
c = strlen(b);
outfile.write(b, c/sizeof(char));
}
infile.close();
outfile.close();
infile.clear();
infile.open("test.txt", ifstream::in);
//读取文件内容
char r = 0;
while(true){
infile >> r;
if (infile.eof())
break;
cout << r;
}
infile.close();
}
test.txt文件内容: