var hFile: THandle; begin hFile := CreateFile('c:\Data.dat', GENERIC_WRITE, 0, Nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED , 0); if (hFile = INVALID_HANDLE_VALUE) then begin MessageBox(GetActiveWindow(),'创建文件失败!',Nil,MB_OK or MB_ICONERROR); Exit; end; end;
const Buffer: AnsiString; var hFile: THandle; iLen: LongWord; begin Buffer := 'F'; hFile := CreateFile('c:\Data.dat', GENERIC_WRITE, 0, Nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED , 0); if (hFile = INVALID_HANDLE_VALUE) then begin MessageBox(GetActiveWindow(),'创建文件失败!',Nil,MB_OK or MB_ICONERROR); Exit; end; SetFilePointer(hFile,1024 * 1024 * 1024 - 1,NIL,FILE_CURRENT); WriteFile( hFile, Buffer[0], Length(Buffer), iLen, Nil); SetEndOfFile(hFile); CloseHandle(hFile); end;
var Buffer: AnsiString;
unsigned: 谢谢大哥了,刚才试了一下发现报错: [Error] Unit1.pas(45): Element 0 inaccessible - use 'Length' or 'SetLength' [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'是不是需要setlength(Buffer,1024 * 1024 * 1024 - 1)?
hFile: THandle;
begin
hFile := CreateFile('c:\Data.dat',
GENERIC_WRITE,
0,
Nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED ,
0);
if (hFile = INVALID_HANDLE_VALUE) then begin
MessageBox(GetActiveWindow(),'创建文件失败!',Nil,MB_OK or MB_ICONERROR);
Exit;
end;
end;
var
hFile: THandle;
iLen: LongWord;
begin
Buffer := 'F';
hFile := CreateFile('c:\Data.dat',
GENERIC_WRITE,
0,
Nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED ,
0);
if (hFile = INVALID_HANDLE_VALUE) then begin
MessageBox(GetActiveWindow(),'创建文件失败!',Nil,MB_OK or MB_ICONERROR);
Exit;
end;
SetFilePointer(hFile,1024 * 1024 * 1024 - 1,NIL,FILE_CURRENT);
WriteFile( hFile, Buffer[0], Length(Buffer), iLen, Nil); SetEndOfFile(hFile);
CloseHandle(hFile);
end;
谢谢大哥了,刚才试了一下发现报错:
[Error] Unit1.pas(45): Element 0 inaccessible - use 'Length' or 'SetLength'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'是不是需要setlength(Buffer,1024 * 1024 * 1024 - 1)?
hFile: THandle;
iLen: LongWord;
Buffer: AnsiString;
begin
Buffer := 'F';
hFile := CreateFile('d:\Data.dat',
GENERIC_WRITE,
0,
Nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL ,
0);
if (hFile = INVALID_HANDLE_VALUE) then begin
MessageBox(GetActiveWindow(),'创建文件失败!',Nil,MB_OK or MB_ICONERROR);
Exit;
end;
SetFilePointer(hFile,1024 * 1024 * 1024 - 1,NIL,FILE_BEGIN);
WriteFile( hFile, Buffer[1], Length(Buffer), iLen, Nil);
CloseHandle(hFile);
end;抱歉前面的代码多了 FILE_FLAG_OVERLAPPED
hFile: THandle;
iLen: LongWord;
Buffer: AnsiString; Ovl: OVERLAPPED;begin
Buffer := 'F';
FillChar(Ovl, sizeof(Ovl), 0);
hFile := CreateFile('d:\Data.dat',
GENERIC_WRITE,
0,
Nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED ,
0);
if (hFile = INVALID_HANDLE_VALUE) then begin
MessageBox(GetActiveWindow(),'创建文件失败!',Nil,MB_OK or MB_ICONERROR);
Exit;
end;
Ovl.Offset := 1024 * 1024 *1024 -1;
Ovl.hEvent := CreateEvent(Nil, true, true, Nil);
WriteFile( hFile, Buffer[1], Length(Buffer), iLen, @Ovl);
GetOverlappedResult( hFile, Ovl, iLen, true);
CloseHandle(Ovl.hEvent);
CloseHandle(hFile);
end;
所以最有效的办法是换个更快的硬盘,或者在大内存机器上往ram disk写
hFile: THandle;
iLen: LongWord;
Buffer: AnsiString;
Ovl: OVERLAPPED;
start,end_,pfmfreq: Int64;
begin
QueryPerformanceCounter(start); hFile := CreateFile('d:\Data.dat',
GENERIC_WRITE,
0,
Nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_NO_BUFFERING or FILE_FLAG_OVERLAPPED ,
0);
if (hFile = INVALID_HANDLE_VALUE) then begin
MessageBox(GetActiveWindow(),'创建文件失败!',Nil,MB_OK or MB_ICONERROR);
Exit;
end;
FillChar(Ovl, sizeof(Ovl), 0);
SetLength(Buffer,512);
FillChar(Buffer[1], 512, 0);
Buffer[512] := 'F';
Ovl.Offset := 1024 * 1024 * 1024 - 512;
Ovl.hEvent := CreateEvent(Nil, true ,true, Nil);
WriteFile( hFile, Buffer[1], Length(Buffer), iLen, @Ovl);
GetOverlappedResult(hFile, Ovl, iLen, true);
CloseHandle(Ovl.hEvent);
CloseHandle(hFile);
QueryPerformanceCounter(end_);
QueryPerformanceFrequency(pfmfreq);
ShowMessage(IntToStr(((end_ - start) * 1000 div pfmfreq)));
end;
那有没有其他办法,请教僵哥了!
我看到pps的方式非常快,真有点不可思议。
CreateFile
SetFilePointer
SetEndOfFile
CloseHandle
总共一毫秒就可以完成.
// 以前写过的一个小软件有这个功能,不过是c++的,不会该delphi,fileSize单位MB
// 刚才试了一下光写入1G文件大概30秒左右
void __fastcall TMakeDummy::MakeFile(const String &fileName, int fileSize)
{
int fileH = 0;
int bufSize = 1024*1024; // 1MB try
{
boost::scoped_array<char> buf(new char[bufSize]);
std::memset(buf.get(), '\0', bufSize); fileH = FileCreate(fileName); for (int i=1; i<=fileSize; i++)
{
FileWrite(fileH, buf.get(), bufSize);
Application->ProcessMessages();
}
// 填充完,把最后一个字节改为0x0F
}
catch (...)
{
FileClose(fileH);
throw 0;
} FileClose(fileH);
}
你这个还得先在DELPHI里实现C++里智能指针