Organize file descriptors, file pointers, and file handles in the C language environment of Windows

This article is a reprint of the article I wrote. http://d.hatena.ne.jp/temtan/20120723/1343050321

When dealing with file input / output in the C language environment of Windows, there are multiple methods and it is easy to get confused, so I tried to organize it.

In the case of Windows C language environment, there are multiple file input / output functions, which are low-level models that imitate C language standard file input / output functions, Windows-specific file handles, and Unix-based functions. There is file input / output.

File pointer
Pointer to the C language structure FILE. The one to get by opening a file with the fopen function. The type is FILE *
File handle
Windows OS specific handle. The one you get with the CreateFile function. The type is HANDLE
File descriptor
A value of type int, called a file descriptor, file descriptor, etc. Equivalent to the one used in UNIX.

Since each function is handled separately, it is not possible to use the C language standard file input / output functions, for example, using a Windows-specific file handle.

The file pointer should always be available in an environment where C language can be used as standard in C language, and the file descriptor described later had a problem with portability, but here it is highly portable and has convenient functions. It is easy to handle because it is hung. When studying C language, I think I will study this first. I was also this at first.

File handles are managed by Windows OS-specific handles. Even when dealing with the above two on Windows, it will eventually be dealt with via this internally (I think). If you access the file here, it's just a hassle. Since there are many arguments and parameters specified in the arguments of Win32API functions, it cannot be handled without looking at the reference one by one. Instead, if you want fine control, you can often do it only here.

The file descriptor is an integer value given by the OS when working with streams on UNIX. Not only files but also streams in general are handled and the handling is low level, so there are some parts that are troublesome to handle. Also, this concept is basically non-portable as it may not be a non-UNIX OS.

Type File pointer File handle File descriptor
Type FILE* HANDLE int
Open file fopen CreateFile _open
Read fread etc. ReadFile _read (Note:Not read)
write fwrite etc. WriteFile _write (Note:not write)

Note that functions that use file descriptors will have an underscore first. No underscore read and write are functions for windows socket and cannot be used for files. It's easy to get confused around here.

And about those functions that convert to each other.

stdio.h io.h
File pointer _fileno File descriptor _get_osfhandle File handle
File pointer _fdopen File descriptor _open_osfhandle File handle

In my case, instead of simply opening the file, there are cases where the pipe is already open, the file is opened in a strange way, and there are various cases where I get it with the file handle, but the function for that ReadFile and WriteFile in are not easy to use due to the large number of argument specifications, and I wanted to use the method using the file pointer that I am accustomed to, so in such cases I use the above function to convert and use it.

Recommended Posts