C++ 讀寫大檔案程式碼
阿新 • • 發佈:2019-01-30
可讀寫大於4G的檔案,測試檔案為vs2013安裝包,大小為7.08G。程式為64bit。
#include "stdafx.h"
#include <windows.h>
#include <memory.h>
#include <iosteam>
#include <fstream>
using namespace std;
const char FILE_RD = "d:\\vs2013.iso";//原檔案
const char FILE_WR = "d:\\vs2013_bak.iso";//待寫入的檔案
const size_t WR_SIZE = 1024 *1024*10;//每次讀寫的大小,此處為10M
void hugefile_rw()
{
FILE* stream_rd,*stream_wr;
size_t num_read,num_written;
int rw_cnt = 0;
long long offset = 0;
if(0 != fopen_s(&stream_wr,FILE_WR,"wb")){//獲取待寫入檔案的控制代碼
return;
}
DWORD dStart = GetTiclCount();
char *buf = new char[WR_SIZE];
if(0 == fopen_s(&stream_rd,FILE_RD,"rb")){
while(!feof(stream_rd)){
rw_cnt++;
//讀
num_read = fread(buf,sizeof(char),WR_SIZE,stream_rd);
if(0==num_read) break;
offset += num_read;//檔案偏移
_fseeki64(stream_rd,offset,SEEK_SET);
//寫
num_written = fwrite(buf,sizeof(char),num_read,stream_wr);
_fseeki64(stream_wr,offset,SEEK_SET);
cout<<"Cnt = "<<rw_cnt<<",num_read = "<<num_read <<",num_written = "<< num_written;
}
}
DWORD dEnd = GetTiclCount();
cout<<"讀寫時間 = "<< (dEnd - dStart)/1000<<"s"<<endl;
//資源釋放
delete []buf;
fclose(stream_rd);
fclose(stream_wr);
}
int main(int argc,_TCHAR* argv[])
{
hugefile_rw();
getchar();
return 0;
}
//if(0 == ferror(stream_wr)){
//ok
//}
用fread讀“.dat”檔案時不能讀完整個檔案,只要是讀到0x1A時,fread就認為結束!用text模式開啟檔案時,系統預設CTRL+Z為檔案結束符,而0x1A剛好就是CTRL+Z的ASCII碼。以以下格式開啟檔案。
datafile = fopen(“whatever.dat”, “rb”); 這樣就可以解決以上問題!
測試可行,但是速度不是很好,可以考慮多執行緒或者記憶體對映檔案方式以提高效率。
每次讀寫1G效率低,100M好些,10M更好些,1M,512k 速度差不多,可能是區域性性原理,快取的原因。
也可以用fstream的方式,程式碼如下:
#include "stdafx.h"
#include <windows.h>
#include <memory.h>
#include <iosteam>
#include <fstream>
using namespace std;
const char FILE_RD = "d:\\vs2013.iso";//原檔案
const char FILE_WR = "d:\\vs2013_bak.iso";//待寫入的檔案
const size_t WR_SIZE = 1024*1024*10;//每次讀寫的大小,此處為10M
void hugefile_rw2()
{
DWORD dStart = GetTiclCount();
fstream in(FILE_RD,ios::in | ios::binary);
fstream out(FILE_WR,ios::out | ios::binary);
// ifstream in(FILE_RD,ios::in | ios::binary);
// ofstream out(FILE_WR,ios::out | ios::binary);
DeleteFileA(FILE_WR);
char *buf = new char[RD_SIZE];
while(!in.eof()){
std::streamsize num_read = in.read(rd_buf,RD_SIZE).gcount();
if(0==size) break;
in.seekg(size,ios::cur);
out.write(rd_buf,size);
out.seekg(size,ios::cur);
}
DWORD dEnd = GetTiclCount();
cout<<"Time = "<< (dEnd - dStart)/1000<<endl;
delete []buf;
in.close();
out.close();
}
int main(int argc,_TCHAR* argv[])
{
hugefile_rw2();
getchar();
return 0;
}
測試發現 fstream 比 fwrite 效率低。