Introduction of File Handling in C
- When data is stored using variables, the data is lost/disappear when the program exits, unless something is done to save it. Thus, File handling in C supports storing data permanently in the created file.
- File handling in C is responsible for the creation of files with proper maintenance and finally leads to the birth of DBMS.
Definition of File Handling in C
- A File is a sequential stream of bytes ending with an end-of-file marker.
- A file represents a sequence of bytes on the disk where a group of related data is stored.
Features/Characteristics of File Handling in C
- File Handling in C means different types of operations applied to C files.
- File Handling in C includes new file creation, opening, and closing of files, storing data in files permanently, modifying data from files, retrieving data from a file, etc.
- Each file ends either with an end-of-file marker or at a specified byte number recorded in a system-maintained, administrative data structure.
- FILE
- It is a kind of type/structure that holds information about the file.
- A File can be used to store a large volume of persistent data.
- The file is created for the permanent storage of data.
- It is a readymade structure. In C language, and we use a structure pointer of the file type to declare a file.
- File Pointer
- A file pointer is a pointer to a structure, that contains information about the file, including its name, the current position of the file, whether the file is being read or written, and whether errors or end of the file have occurred.
File Structure
- Streams of File –
- Streams provide communication channels between files and programs.
- When the file is opened, the stream is associated with the file.
- By default, three files and their streams are automatically opened when program execution begins – the standard input, standard output, and the standard error.
- The standard input, standard output, and the standard error are manipulated using file pointers stdin, stdout, and stderr.
- The standard input stream enables a program to read data from the keyboard.
- The standard output stream enables to write data on the screen.
- Buffered File System –
- This file system is referred to as buffered because the routines maintain all the disk buffers required for reading/writing automatically.
File Types
- File Handling in C supports two types of files, based on storage of contents – (A) Binary files and (B)Text files.
-
Binary Files –
- A binary file contains data that was written in the same format used to store it internally in the main memory. For example, the integer value 1500 will be stored in 2 bytes depending on the machine while it will require 5 bytes in a text file.
- The binary files are easier to handle.
- In binary files, no special string for numeric conversions is necessary.
-
Text Files –
- In text files, everything is stored in terms of text i.e. even if we store a numeric/integer value 23, it will be stored as a 3-byte string – “23\0”.
- In a text file, certain character translations may occur. For example, a newline(\n) character may be converted to a carriage return, linefeed pair.
-
- C also has two types of files – (I) Buffered file System and (II) Unbuffered File System
(I) Buffered file System
-
-
- The buffered file system uses buffered input and output i.e. the operating system handles the details of data retrieval and storage,
- The system stores data temporarily (in buffers) to optimize file system access.
-
(II) Unbuffered file System
-
-
- The unbuffered I/O functions are handled directly as system calls without buffering the data by the operating system. That is why they are also known as low-level functions.
- The low-level functions are defined in the header file <io.h>.
- These functions do not use file pointer of type FILE to access a particular file, but they use directly the file descriptors, of type integer. They are also called handles.
- This is referred to as an unbuffered I/O system because the programmer must provide and maintain all disk buffers, the routines do not do it automatically.
-
- C also has two types of files, based on file access – (a) Sequential file and (b) Random file
(a) Sequential File
-
-
- Sequential access files allow reading the data from the file in a sequential/linear manner i.e. data can only be read/searched in sequence.
-
(b) Random File
-
-
- Random access files allow reading/searching data from any location in the file.
- To do this, C defines a set of functions(such as fseek(), rewind(), ftell(), etc.) to manipulate the position of the file pointer.
-
File Pointer
- To access any file, we need to declare a pointer to the FILE structure and then associate it with the particular file. This pointer is referred to as a file pointer.
- It is declared as –
FILE *fp; - Opening a file returns a pointer to a FILE structure (defined in <stdio.h>) that contains information, such as size, current file pointer position, type of file, etc., to perform operations on the file. This structure also contains an integer called a file descriptor which is an index into the table maintained by the operating system namely, the open file table. Each element of this table contains a block called file control block (FCB) used by the operating system to administer a particular file.
File Handling Built-in Functions
-
fopen()
- To do any operation in a file, it must be open first.
- This function is used for creating a file or opening an existing file.
- fopen() is a standard function that is normally used to open a file. In other words, If the specified file is not present on the system, then it is created and then opened and when the specified file is already present on the system, then it is directly opened using this function.
- The fopen() function opens a stream for use and links a file with that stream.
- fp is a file pointer that points to the type file i.e. this function returns a file pointer.
- The syntax is as –
FILE *fopen(char *filename, *openingmode);
(where filename is the name of file that is a string of characters that provide a valid file name for the operating system and may include a path specification and opening mode is a string, containing the desired open status.) - The value returned by the fopen( ) function is a file pointer.
- If any error occurs while opening the file, the value of the file pointer is NULL, a constant declared in <stdio.h> file.
- After opening the file, the next thing needed is the way to read or write the file.
- There are several functions and macros defined in <stdio.h> header file for reading and writing the file. These functions can be categorized according to the form and type of data read or written onto a file. The list of several file functions is discussed in this topic.
- List of File Opening Mode –
r / rt – opens a text file for read only mode/process.
w / wt – creates a text file for write only mode/process.
a / at – appends a file to text file.
r+t – opens a text file for read and write mode/process both.
w+t – creates a text file in read and write mode.
a+t – opens/creates a text file in read mode.
rb – opens a binary file in read only mode.
wb – creates a binary file in write only mode.
ab – appends a file in a binary file.
r+b – opens a binary file in read and write mode.
w+b – creates a binary file in read and write mode.
a+b – opens a binary file in read mode.
-
fclose()
- It is suggested that an open file must be closed properly when the processing of the file is finished.
- Syntax is :
int fclose(FILE *fptr); - This function flushes any unwritten data for the stream, discards any unread buffered input, frees any automatically allocated buffer, and then closes the stream.
- It returns 0 if the file is closed successfully or a constant EOF, an end-of-file marker if an error occurred. This constant is also defined in <stdio.h>.
- If the function fclose() is not called explicitly, the operating system normally will close the file when the program execution terminates.
- Once the file is closed, it cannot be used further. If required it can be opened again in the same or another mode.
-
feof()
- It is the C library file handling function that checks/tests/finds the end-of-file indicator for the given stream for a file.
- The purpose of the C feof function is used to determine whether the end of the file (stream), specified has been reached or not.
- This function keeps on searching from the beginning till the end of file (eof), by moving forward, in our file program.
- It returns the value zero when the end of the file has not occurred, otherwise, it returns 1.
- Syntax :
int feof(FILE *stream/*filename);
-
eof() [End of file]
- EOF is an end-of-file marker.
- It is a macro defined in <stdio.h>.
- It is a macro definition of type int that expands into a negative integral constant expression (generally, -1) i.e. Its value is –1.
- It returns non-zero when the end of the file has been reached, otherwise, it returns zero.
- EOF marks are helpful in data transmission and storage. Files are stored in blocks, and the end marker helps the computer to know it has allocated enough space to store the file.
-
getc()
- getc( ) is used to read a single character at a time from a file.
- This is an ANSI C function for reading character by character or one byte at a time from a file.
- This function is defined in the standard library i.e. in <stdio.h> as macros, not functions.
- The syntax is as follows –
int getc(FILE *stream); - The file pointer indicates the file to read from it.
- getc( ) returns an integer value but only the low-order byte is used.
- It returns EOF when the end-of-file is reached.
- getc( )is defined in <stdio.h> as macros not functions.
-
putc()
- putc( ) is used to write/put a single character at a time to a file.
- This is an ANSI C function for writing character by character or one byte at a time into a file.
- This function is defined in the standard library i.e. in <stdio.h> as macros, not functions.
- The syntax is as follows –
int putc(int ch, FILE *stream); - The file pointer indicates the file to write into it.
- The character ‘ch’ discussed in the syntax above is formally called an integer in putc( ) function but only the low-order byte is used. On success putc( ) returns a character(in integer form) written or EOF on failure.
- putc( ) is defined in <stdio.h> as macros not functions.
-
putw()
- Writing an integer to a file.
-
getw()
- Reads an integer from a file.
-
fgetc()
- This function is used to read a character from a file at a time.
- After reading a character it moves the file pointer position to the next address/ location to read the next character.
- It returns the read character pointed by the file pointer, if successful otherwise, returns EOF.
- fgetc cannot be implemented as a macro whereas getc can be.
- Syntax :
int fgetc(FILE *fp);
-
fputc()
- This function is used to write a single character at a time to a given file.
- The character that is written inside the text file is specified by the first argument of the function pointed by the fp pointer.
- After writing a character to the text file, it increments the position of the pointer.
- It returns the write character pointed by the file pointer, if successful otherwise, returns EOF.
- Syntax:
int fputc(int ch, FILE *fp);
-
fgets()
- This file-handling string function is used to read a whole line in the file.
- C has some string input functions with the help of which we can read a set of characters at one time.
- This function is defined in the C standard library.
- Syntax :
char *fgets(char *str, int num, FILE *stream);
(Where the integer parameter in fgets( ) (int num) is used to indicate that at most num -1 characters are to be read from the file, terminating at end-of-file or end-of-line. The end-of-line character will be placed in the string str before the string terminator if it is read. If end-of-file is encountered as the first character, EOF is returned, otherwise, str is returned.)
-
fputs()
- This file-handling string function is used to write a whole line in the file.
- C has some string output functions with the help of which we can write a set of characters at one time.
- This function is defined in the C standard library.
- Syntax :
int fputs(char *str, FILE *stream);
(Where the end-of-line character will be placed in the string str before the string terminator if it is read. If end-of-file is encountered as the first character, EOF is returned, otherwise, str is returned. The fputs( ) function returns a non-negative number or EOF if unsuccessful.)
-
fscanf()
- When a file contains data in the form of digits, real numbers, characters, and strings i.e. mix of all these, then character input functions are not enough as the values would be written in the form of characters. Hence C provides a specialized formatted input function called fscanf().
- These are defined in the C standard library.
- This function is applied for formatted input i.e. reading a block of data from a file.
- This is identical to scanf() except that the first argument is a file pointer that specifies the file to be written, and the second argument is the format string.
- Syntax :
int fscanf(FILE *fp, char *format,. . .);
-
- This function returns an integer indicating the number of bytes written.
-
fprintf()
- When a file contains data in the form of digits, real numbers, characters and strings i.e. mix of all these, then character read functions are not enough as the values would be read in the form of characters. Hence C provides a specialized formatted output function called fprintf().
- These are defined in the C standard library.
- This function is applied for formatted output i.e. writing a block of data to a file.
- This is identical to printf() except that the first argument is a file pointer that specifies the file to be read, and the second argument is the format string.
- Syntax :
int fprintf(FILE *fp, char *format,. . .);
-
- This function returns an integer indicating the number of bytes read.
-
read()
- The read() function reads data previously written to a file.
-
write()
- The write function returns the number of bytes successfully written into the array, which may at times be less than the specified no.of bytes.
- It returns -1 if an error is encountered.
-
fread()
- This is an output function that reads a block of data(a specific number of bytes) from a file. A block can be a record, a set of records or an array.
- This function is defined in the C standard library.
- This function allows the reading of blocks of data.
- Syntax is :
int fread(void *buf, int num_bytes, int count, FILE *fp);
(Where buf is the pointer to a memory area that receives the data from the file, num_bytes specifies the number of bytes to be read.)
-
- This function is quite helpful in the case of binary files.
- Generally, this function is used to read an array of records from a file.
-
fwrite()
-
- This is an input function that writes a block of data(a specific number of bytes) from a file. A block can be a record, a set of records or an array.
- This function is defined in the C standard library.
- This function allows the writing of blocks of data.
- Syntax :
int fwrite(void *buf, int num_bytes, int count, FILE *fp);
(Where buf is the pointer to the information to be written to the file, num_bytes specifies the number of bytes to be written.)
-
- This function is quite helpful in the case of binary files.
- Generally, this function is used to write an array of records from a file.
-
lseek()
- The function lseek() is used to move to the specific position in a file.
- Its Syntax/Prototype is :
long lseek(int fd, long offset, int pos); - This function is the same as fseek() except that the file descriptor is used instead of the file pointer.
- It is possible to write any kind of program dealing with files.
-
fseek()
- To support random access files, C requires a function with the help of which the file pointer can be positioned at any random location in the file. Such a function is defined in the C standard library.
- The function fseek( ) is used to set the position of a file pointer to a specified location.
- Its Syntax/Prototype is :
int fseek(FILE *fp, long offset, int pos);
(Where the first argument fp is the pointer to a file. The second argument is the number of bytes to move the file pointer, counting from zero. This argument can be positive, negative, or zero depending on the desired movement. The third parameter is a flag indicating from where in the file to compute the offset. It can have three values:-
SEEK_SET(or value 0) at the beginning of the file,
SEEK_CUR(or value 1) the current position and
SEEK_END(or value 2) the end of the file
These three constants are defined in <stdio.h>.)
-
- If the process is successful fseek( ) returns zero.
- ftell()
- The function ftell() is used to give the current position of a file pointer.
- Its Syntax/Prototype is :
long ftell(FILE *fp); - It returns –1 on error and the position of the file pointer if successful.
-
rewind()
- The function rewind() is used to reset the file pointer position to the beginning of the file.
- Its syntax/prototype is :
void rewind(FILE *fp); -
A call to rewind is equivalent to the call
0 Comments