win32 与 C库 复制文件的比较
2009年10月18日
(1)C库复制(2)windows实现(3)windows函数调用
以这三者的进行复制gogolepinyininstaller.exe(谷歌拼音输入法9.783 KB)进行测试.
(1)cpC.c
[b]#include
#include
#include
#define BUF_SIZE 256
int main (int argc, char *argv []) {
clock_t start_t, finish_t ;
FILE *in_file, *out_file ;
char rec [BUF_SIZE] ;
size_t bytes_in, bytes_out ;
start_t = clock () ;
if (argc != 3) {
printf ("Usage: cpC file1 file2\n") ;
return 1 ;
}
in_file = fopen (argv [1], "rb") ;
if (in_file == NULL) {
perror (argv [1]) ;
return 2 ;
}
out_file = fopen (argv[2], "wb") ;
if (out_file == NULL) {
perror (argv [2]) ;
return 3 ;
}
while ((bytes_in = fread (rec, 1, BUF_SIZE, in_file)) > 0) {
bytes_out = fwrite (rec, 1, bytes_in, out_file) ;
if (bytes_out != bytes_in) {
perror ("Fatal write error.") ;
return 4 ;
}
}
fclose (in_file) ;
fclose (out_file) ;
finish_t = clock () ;
printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
return 0 ;
}
(2)cpW.c
[/b][b]#include
#include
#include
#define BUF_SIZE 256
int main (int argc, LPTSTR argv []) {
clock_t start_t, finish_t ;
HANDLE hIn, hOut ;
DWORD nIn, nOut ;
CHAR Buffer [BUF_SIZE] ;
start_t = clock() ;
if (argc != 3) {
printf ("Usage: cpW file1 file2\n") ;
return 1 ;
}
hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL) ;
if (hIn == INVALID_HANDLE_VALUE) {
printf ("Cannot open input file. Error: %x\n", GetLastError ()) ;
return 2 ;
}
hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (hOut == INVALID_HANDLE_VALUE) {
printf ("Cannot open output file. Error: %x\n", GetLastError ()) ;
return 3 ;
}
while (ReadFile (hIn, Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) {
WriteFile (hOut, Buffer, nIn, &nOut, NULL) ;
if (nIn != nOut) {
printf ("Fatal write error: %x\n", GetLastError ()) ;
return 4 ;
}
}
CloseHandle (hIn) ;
CloseHandle (hOut) ;
finish_t = clock () ;
printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
return 0 ;
}[/b]
(3)cpF.c
[b]#include
#include
#include
int main (int argc, LPTSTR argv []) {
clock_t start_t, finish_t ;
start_t = clock () ;
if (argc != 3) {
printf ("Usage: cpF file1 file2\n") ;
return 1 ;
}
if (!CopyFile (argv [1], argv [2], FALSE)) {
printf ("CopyFile Error: %x\n", GetLastError ()) ;
return 2 ;
}
finish_t = clock () ;
printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
return 0 ;
}[/b]
下面,输入比较的结果
2009年10月18日
(1)C库复制(2)windows实现(3)windows函数调用
以这三者的进行复制gogolepinyininstaller.exe(谷歌拼音输入法9.783 KB)进行测试.
(1)cpC.c
[b]#include
#include
#include
#define BUF_SIZE 256
int main (int argc, char *argv []) {
clock_t start_t, finish_t ;
FILE *in_file, *out_file ;
char rec [BUF_SIZE] ;
size_t bytes_in, bytes_out ;
start_t = clock () ;
if (argc != 3) {
printf ("Usage: cpC file1 file2\n") ;
return 1 ;
}
in_file = fopen (argv [1], "rb") ;
if (in_file == NULL) {
perror (argv [1]) ;
return 2 ;
}
out_file = fopen (argv[2], "wb") ;
if (out_file == NULL) {
perror (argv [2]) ;
return 3 ;
}
while ((bytes_in = fread (rec, 1, BUF_SIZE, in_file)) > 0) {
bytes_out = fwrite (rec, 1, bytes_in, out_file) ;
if (bytes_out != bytes_in) {
perror ("Fatal write error.") ;
return 4 ;
}
}
fclose (in_file) ;
fclose (out_file) ;
finish_t = clock () ;
printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
return 0 ;
}
(2)cpW.c
[/b][b]#include
#include
#include
#define BUF_SIZE 256
int main (int argc, LPTSTR argv []) {
clock_t start_t, finish_t ;
HANDLE hIn, hOut ;
DWORD nIn, nOut ;
CHAR Buffer [BUF_SIZE] ;
start_t = clock() ;
if (argc != 3) {
printf ("Usage: cpW file1 file2\n") ;
return 1 ;
}
hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL) ;
if (hIn == INVALID_HANDLE_VALUE) {
printf ("Cannot open input file. Error: %x\n", GetLastError ()) ;
return 2 ;
}
hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (hOut == INVALID_HANDLE_VALUE) {
printf ("Cannot open output file. Error: %x\n", GetLastError ()) ;
return 3 ;
}
while (ReadFile (hIn, Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) {
WriteFile (hOut, Buffer, nIn, &nOut, NULL) ;
if (nIn != nOut) {
printf ("Fatal write error: %x\n", GetLastError ()) ;
return 4 ;
}
}
CloseHandle (hIn) ;
CloseHandle (hOut) ;
finish_t = clock () ;
printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
return 0 ;
}[/b]
(3)cpF.c
[b]#include
#include
#include
int main (int argc, LPTSTR argv []) {
clock_t start_t, finish_t ;
start_t = clock () ;
if (argc != 3) {
printf ("Usage: cpF file1 file2\n") ;
return 1 ;
}
if (!CopyFile (argv [1], argv [2], FALSE)) {
printf ("CopyFile Error: %x\n", GetLastError ()) ;
return 2 ;
}
finish_t = clock () ;
printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
return 0 ;
}[/b]
下面,输入比较的结果