1 of 31

FILE HANDLING IN C

2 of 31

TYPES OF FILES�

  • When dealing with files, there are two types of files you should know about:
  • Text files
  • Binary files

3 of 31

 TEXT FILES�

  • Text files are the normal .txt files. We can easily create text files using any simple text editors such as Notepad.
  • When We open those files, we'll see all the contents within the file as plain text. We can easily edit or delete the contents.

4 of 31

TEXT FILES�

  • File handling in C is the process in which we create, open, read, write, and close operations on a file.
  • C provides different functions such as fread()fseek()fprintf(), etc. to perform input, output, and many different C file operations in our program.

5 of 31

OPENING A FILE

  • Before we perform any operations on a file, we need to open it.
  • We do this by using a file pointer.
  • The type FILE defined in stdio.h allows us to define a file pointer. Then you use the function fopen() for opening a file.
  • Once this is done one can read or write to the file using the fread() or fwrite() functions, respectively.

6 of 31

2. BINARY FILES�

  • Binary files are mostly the .bin files in your computer.

  • Instead of storing data in plain text, they store it in the binary form (0's and 1's).

7 of 31

FILE OPERATIONS�

  • In C, we can perform four major operations on files, either text or binary:
  • Creating a new file
  • Opening an existing file
  • Closing a file
  • Reading from and writing information to a file

8 of 31

WORKING WITH FILES�

  • When working with files, you need to declare a pointer of type file. This declaration is needed for communication between the file and the program.
  • FILE *fptr;

9 of 31

OPENING A FILE - FOR CREATION AND EDIT�

  • Opening a file is performed using the fopen() function defined in the stdio.h header file.
  • The syntax for opening a file in standard I/O is:

10 of 31

SYNTAX OF FOPEN()�

  • FILE* fopen(*file_name, *access_mode);
  • Parameters
  • file_name: name of the file when present in the same directory as the source file. Otherwise, full path.
  • access_modeSpecifies for what operation the file is being opened.

11 of 31

CREATE/OPEN A FILE

  • Example

  • fp = fopen(“one.txt", "r");

12 of 31

RETURN VALUE

  • If the file is opened successfully, returns a file pointer to it.

  • If the file is not opened, then returns NULL.

13 of 31

FILE OPENING MODES�

  • File opening modes or access modes specify the allowed operations on the file to be opened.
  • They are passed as an argument to the fopen() function. Some of the commonly used file access modes are listed below:

14 of 31

FILE ACCESS_MODE

  • File Mode Description
  • r Open a text file for reading
  • w Create a text file for writing, if it exists, it is overwritten.
  • a Open a text file and append text to the end of the file.
  • rb Open a binary file for reading
  • wb Create a binary file for writing, if it exists, it is overwritten.
  • ab Open a binary file and append data to the end of the file.

15 of 31

FILE ACCESS_MODE

  • r+ Open the existing file for both reading and writing

  • w+ Open the file for both reading and writing

  • a+ Open the file for both appending and reading
  •  

16 of 31

FCLOSE()

  • The fclose() function is used for closing opened files. The only argument it accepts is the file pointer.
  • If a program terminates, it automatically closes all opened files. But it is a good programming habit to close any file once it is no longer needed.

  • fclose(FILE *fp);

17 of 31

WRITE TO A FILE�

  • The file write operations can be performed by the functions fprintf() and fputs().
  • C programming also provides some other functions that can be used to write data to a file such as:

18 of 31

Function

Description

Similar to printf(), this function uses formatted string and variable arguments list to print output to the file.

Prints the whole line in the file and a newline at the end.

Prints a single character into the file.

fputw()

Prints a number to the file.

fwrite()

This function writes the specified number of bytes to the binary file.

19 of 31

READING FROM A FILE�

  • The file read operation in C can be performed using functions fscanf() or fgets().
  • Both the functions performed the same operations as that of scanf() and gets but with an additional parameter, the file pointer.
  • There are also other functions we can use to read from a file. Such functions are listed below:

20 of 31

READING FROM A FILE

Function

Description

Use formatted string and variable arguments list to take input from a file.

Input the whole line from the file.

Reads a single character from the file.

fgetw()

Reads a number from a file.

Reads the specified bytes of data from a binary file.

21 of 31

READING AND WRITING TO A BINARY FILE

  • Functions fread() and fwrite() are used for reading from and writing to a file on the disk respectively in case of binary files.
  •  

22 of 31

WRITING TO A BINARY FILE�

  • To write into a binary file, we need to use the fwrite() function. The functions take four arguments:
  • address of data to be written in the disk
  • size of data to be written in the disk
  • number of such type of data
  • pointer to the file where we want to write.

23 of 31

SYNTAX

  • fwrite(addressData, sizeData, numbersData, pointerToFile);

24 of 31

READING FROM A BINARY FILE�

  • Function fread() also take 4 arguments similar to the fwrite() function as above.
  • fread(addressData, sizeData, numbersData, pointerToFile);

25 of 31

GETTING DATA USING FSEEK()�

  • The fseek() function is used to move the file pointer associated with a given file to a specific location in the file.
  • It provides random access capabilities, which allow the programmer to randomly move the file pointer to any location in the file to read or write data.

26 of 31

FSEEK

Position

Meaning

SEEK_SET

Starts the offset from the beginning of the file.

SEEK_END

Starts the offset from the end of the file.

SEEK_CUR

Starts the offset from the current location of the cursor in the file.

27 of 31

SYNTAX OF FSEEK()�

  • fseek(FILE * stream, long int offset, int position);

28 of 31

FTELL()

  • The ftell() function in C is part of the standard input/output library (<stdio.h>) and is used to retrieve the current position of the file position indicator for a specified file stream. 
  •  ftell() is used to determine the position of the file pointer in the file from the beginning of the file
  • This position is typically measured in bytes from the beginning of the file.
  • syntax
  • long int ftell(FILE *stream);

29 of 31

FGETC()

  • fgetc() is used to obtain input from a file single character at a time. 
  • int fgetc(FILE *pointer)

30 of 31

FGETS() FUNCTION

  • The fgets() function in C is a standard library function used for reading a string from a specified input stream. It is part of the <stdio.h> header file and provides a safer alternative to gets() by preventing buffer overflows.
  • char *fgets(char *str, int n, FILE *stream);

31 of 31

FSCANF (FILE SCAN FORMATTED):�

  • Formatted input: 
  • fscanf reads formatted input from a file stream, attempting to match the input with a specified format string. It can directly convert text representations to various data types (integers, floats, strings, etc.).
  • fscanf(FILE *stream, const char *format, ...);