C++二进制文件读写
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 int main() 5 { 6 char str1[20]; 7 char str2[20]; //定义读写两个文件的空间 8 cin >> str1 >>str2;//从键盘读入文件名 9 ifstream in(str1,ios::binary); 10 ofstream out(str2,ios::binary);//打开文件 11 if( !in )//文件打开失败异常处理 12 { 13 cerr << "in????????!" << endl; 14 return 0; 15 } 16 if( !out ) 17 { 18 cerr << "out????????!" << endl; 19 return 0; 20 } 21 char x; 22 while( !in.eof())//检查是否到文件尾 23 { 24 in.get(x);//从文件流中读 25 out.put(x);//往文件流写入 26 } 27 in.close(); 28 out.close();//释放文件指针 29 return 0; 30 }
上面这段代码可以指定一个二进制文件创建副本,代码非常简单。
可以参考
浙公网安备 33010602011771号