-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_readwrite.c
More file actions
68 lines (57 loc) · 1.59 KB
/
test_readwrite.c
File metadata and controls
68 lines (57 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <windows.h>
#include <stdio.h>
int main() {
printf("Testing CreateFile + WriteFile + ReadFile\n");
// Create/open file
HANDLE hFile = CreateFileA(
"test_data.txt",
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
printf("CreateFileA failed!\n");
return 1;
}
printf("File created: handle=%p\n", hFile);
// Write data
const char* writeData = "Hello from LSW ReadFile test!";
DWORD bytesWritten = 0;
if (!WriteFile(hFile, writeData, strlen(writeData), &bytesWritten, NULL)) {
printf("WriteFile failed!\n");
CloseHandle(hFile);
return 1;
}
printf("Wrote %lu bytes\n", bytesWritten);
// Close and reopen for reading
CloseHandle(hFile);
hFile = CreateFileA(
"test_data.txt",
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
printf("CreateFileA for reading failed!\n");
return 1;
}
printf("File reopened for reading\n");
// Read data
char readBuffer[256] = {0};
DWORD bytesRead = 0;
if (!ReadFile(hFile, readBuffer, sizeof(readBuffer) - 1, &bytesRead, NULL)) {
printf("ReadFile failed!\n");
CloseHandle(hFile);
return 1;
}
printf("Read %lu bytes: '%s'\n", bytesRead, readBuffer);
CloseHandle(hFile);
printf("Test complete!\n");
return 0;
}