
header file is required in order to use the mode values passed to the open function.
Example 6.17 demonstrates use of the open family of functions to create, write to, and
close a file.
Example 6.17 File Handling with Open(), Read(), Write(), and Close() Functions
(file2.c)
1 /*
2 * file2.c
3 *
4 * Win32 open function example
5 */
6
7
#include <stdio.h>
8 #include <io.h> // required for open, write, close functions
9 #include <fcntl.h> // required for open modes (_O_CREAT, etc)
10
11
int
12 main(void)
13 {
14 int ret = 0;
15 int fd = 0;
16
17
fd = open("test.txt", _O_CREAT | _O_WRONLY);
18 if(fd < 0)
19 {
20 printf("open() failed.\r\n");
21 return(1);
22 }
23
24
ret = write(fd, ...