Skip to content

Instantly share code, notes, and snippets.

@jen6
Created July 28, 2015 09:00
Show Gist options
  • Save jen6/f7c4d0e3718243b5cc2b to your computer and use it in GitHub Desktop.
Save jen6/f7c4d0e3718243b5cc2b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <Windows.h>
#include <tchar.h>
#define MAX_SIZE 1024
TCHAR file_name[] = _T("test");
char buf[MAX_SIZE] = { 0, };
int test_fopen()
{
DWORD t = GetTickCount();
FILE * file_read = fopen("test", "rb");
if (file_read == nullptr)
return 1;
FILE * file_write = fopen("cptest1", "wb");
if (file_write == nullptr)
{
fclose(file_read);
return 1;
}
while (fgets(buf, MAX_SIZE, file_read) != NULL)
{
fwrite(buf, sizeof(char), MAX_SIZE, file_write);
}
fclose(file_read);
fclose(file_write);
printf("fopen : %d\n", GetTickCount() - t);
return 0;
}
int test_create_file()
{
DWORD t = GetTickCount();
HANDLE hread, hwrite;
DWORD readn, writen;
BOOL rrtv, wrtv;
hread = CreateFile(file_name, GENERIC_ALL, NULL, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hread == INVALID_HANDLE_VALUE)
{
printf("%d\n", GetLastError());
return 1;
}
hwrite = CreateFile(_T("cptest2"), GENERIC_ALL, NULL, NULL,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hwrite == INVALID_HANDLE_VALUE)
{
printf("%d\n", GetLastError());
CloseHandle(hread);
return 1;
}
while (1)
{
rrtv = ReadFile(hread, buf, MAX_SIZE, &readn, NULL);
if (rrtv && readn == 0)
break;
wrtv = WriteFile(hwrite, buf, MAX_SIZE, &writen, NULL);
}
CloseHandle(hread);
CloseHandle(hwrite);
printf("WriteFile : %d\n", GetTickCount() - t);
return 0;
}
int main()
{
bool ok = test_fopen();
if (ok)
{
printf("error in test open\n");
}
memset(buf, 2, MAX_SIZE);
ok = test_create_file();
if (ok)
{
printf("error in test open\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment