level 10
读写磁盘文件的代码已经测试,为了和LINUX兼容,用的是fstream类中的方法
#include <iostream>
#include <fstream>
#include "stdio.h"
using namespace std; int main()
{
ifstream if1("f:\\milktea.jpg",ios::in | ios::binary); //(设置if1为开共享读不开共享写模式##木有了),且是二进制输出
if(!if1) //如果if1创建失败输出报错信息
{
cerr<<"open error!"<<endl;
}
if1.seekg(0,ios::end); //将文件指针移动到这个文件的末尾
long n=0;
n=if1.tellg(); //共有13万个字节
cout<<"文件长度是"<<n<<"个字节"<<endl;
if1.seekg(0,ios::beg); //将文件指针移动到这个文件的开头
char *p=new char[n];
if1.read(p,n); //将文件全部读入到内存上
cout<<"read successful"<<endl;
ofstream of1("f:\\1.jpg",ios::out | ios::binary); //设置二进制形式输出
of1.write(p,n); //写入试试
delete [] p; //释放堆中的字符数组P[n]
if1.close(); //关闭if1和文件的关联 return 0;
}
2012年03月07日 08点03分