hi all
i have a program for reading/writing files using windows API
this program works properly
but i have problem with program speed
the time of reading the file is good but it takes very more time for writing the file
here is the standard source code that my program uses a source like this for reading/writing the file
you can test it on any version of C++Builder (or any other IDE?) and see the resulted time (please only call writefile and readfile functions)
#include <DateUtils.hpp>
HANDLE OpenWrite(const char* fname) {
HANDLE fileHandle = CreateFile( fname,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (fileHandle == INVALID_HANDLE_VALUE)
fileHandle = NULL;
return fileHandle;
}
HANDLE OpenRead(const char* fname) {
HANDLE fileHandle = CreateFile( fname,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (fileHandle == INVALID_HANDLE_VALUE)
fileHandle = NULL;
return fileHandle;
}
void writefile(){
int n[100000];
DWORD dwrt = 0;
HANDLE out=NULL;
out = OpenWrite("test");
if (out == NULL){
ShowMessage("Cannot open file.");
exit(0);
}
for (int pos = 0; pos < 100000; pos++) {
n[pos] = 1;
}
TTime t1=Time ();
WriteFile(out,n,sizeof(n),&dwrt,NULL);
TTime t2=Time ();
ShowMessage("time of writing : "+ IntToStr(MilliSecondsBetween(t1,t2)));
CloseHandle(out);
out = NULL;
}
void readfile(){
int n[100000];
HANDLE in=NULL;
DWORD dwrd = 0;
in = OpenRead("test");
if (in == NULL){
ShowMessage("Cannot open file.");
exit(0);
}
TTime t1=Time ();
ReadFile(in,n,sizeof(n),&dwrt,NULL);
TTime t2=Time ();
ShowMessage("time of reading : "+ IntToStr(MilliSecondsBetween(t1,t2)));
CloseHandle(in);
in = NULL;
}