What is file open in C?

In C, when we open a file, what happens? As I know, the contents of the file are not loaded into memory when the file is opened. Does it just set the file descriptor? So what is this file descriptor? And if the contents of the file are not loaded into memory, then how does the file open?

+6
c file
source share
7 answers

Typically, if you open a file using fopen or (on a POSIX system) open , the function will β€œopen the file” if successful - it simply gives you a value (a FILE * or int ) for use in future calls to the read function.

Under the hood, the operating system may read some or all of the files; it may not be. You must call some function to request the data that you need to read, and if this was not done by the time fread / fgets / read / etc ... was called, then it would be at that moment.

A "file descriptor" usually refers to the integer returned by open on POSIX systems. It is used to identify an open file. If you get the value 3 , somewhere, the operating system keeps track of what 3 refers to /home/user/dir/file.txt or the like. This is a small small value indicating the OS whose file should be read. When you call open and open say, foo.txt , the OS says: "ok, the file is open, calling it 3 here."

+6
source share

This question is not fully related to the programming language. Although the library does affect what happens when the file is opened (for example, using open or fopen ), the main behavior comes from the operating system.

Linux, and I assume that other OSs in most cases read ahead. This means that the file is actually read from the physical storage even before you call read on the file. This is done as an optimization, reducing read time when the file is actually read by the user. This behavior can be partially controlled by the programmer using a special flag for open functions. For example, the Win32 API CreateFile can specify FILE_FLAG_RANDOM_ACCESS or FILE_FLAG_SEQUENTIAL_SCAN to indicate random access (in this case the file is not read in advance) or sequential access (in this case, the OS will perform rather aggressive forward reads), respectively. Other OS APIs may give more or less control.

For the basic ANSI C API open , read , write , which use a file descriptor, the file descriptor is a simple integer that is passed to the OS and designates the file. In the OS itself, this most often translates into some structure that contains all the necessary information for the file (name, path, search for offsets, size, reading and writing buffers, etc.). The OS will open a file, which means searching for a specific entry in the file system ( inode for Linux), which matches the path specified in the open method, creates a file structure and returns an identifier to the user - a file descriptor. From this moment, the OS freely reads any data that seems to be suitable, even if it is not requested by the user (often reading more requests is required in order to at least work in the proper size of the file system).

+4
source share

C has no primitives for file I / O; it all depends on which operating system and which libraries you use.

+2
source share

File descriptors are just abstracts. Everything is done in the operating system.

+1
source share

If the program uses fopen() , then for the buffering packet, an implementation-specific system call will be used to get the file descriptor, and it will save it in the FILE structure.

A system call (at least on Unix, Linux, and Mac) will search (usually) the file system on disk to search for the file. It creates data structures in kernel memory that collect the information needed to read or write a file.

It also creates a table for each process that references other kernel data structures needed to access the file. The index in this table is (usually) a small number. This is a file descriptor that is returned from a system call to a user process and then stored in a FILE structure.

+1
source share

As already mentioned, this is the functionality of the OS.

But for input / output of C files, most likely you need information about the fopen function.

If you check the description for this function, it says:

Description:

Opens a stream.

fopen opens a file called filename and associates the stream with This. fopen returns a pointer that will be used to identify the stream in subsequent operations.

So, on success, fopen simply returns a pointer to the newly opened stream. And it returns NULL in case of any error.

0
source share

When you open a file, the file pointer gets the base address (start address) of this file. Then you use different functions to work with the file. EDIT: Thanks Chris, here is a structure called FILE

 typedef struct { int level; /* fill/empty level of buffer */ unsigned flags; /* File status flags */ char fd; /* File descriptor */ unsigned char hold; /* Ungetc char if no buffer */ int bsize; /* Buffer size */ unsigned char *buffer; /* Data transfer buffer */ unsigned char *curp; /* Current active pointer */ unsigned istemp; /* Temporary file indicator */ short token; /* Used for validity checking */ } FILE; 
0
source share

All Articles