File Handling in C
In programming, we may require some specific input data to be generated several numbers of times. Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again. However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of file handling in C.
File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. The following operations can be performed on a file.
- Creation of the new file
- Opening an existing file
- Reading from the file
- Writing to the file
- Deleting the file
C – Preprocessors
The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. We’ll refer to the C Preprocessor as CPP.
All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column.
The following section lists down all the important preprocessor directives:
- #define: Substitutes a preprocessor macro.
- #include: Inserts a particular header from another file.
- #undef: Undefines a preprocessor macro.
- #ifdef: Returns true if this macro is defined
- #ifndef: Returns true if this macro is not defined
- #if: Tests if a compile time condition is true
- #else: The alternative for #if
- #elif: #else and #if in one statement
- #endif: Ends preprocessor conditional
- #error: Prints error message on stderr.
- #pragma: Issues special commands to the compiler, using a standardized method