File Handling

Notes Zone
0

 File Handling

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

Why do we need File Handling in C?

So far the operations using the C program are done on a prompt/terminal which is not stored anywhere. The output is deleted when the program is closed. But in the software industry, most programs are written to store the information fetched from the program. The use of file handling is exactly what the situation calls for.

In order to understand why file handling is important, let us look at a few features of using files:

  • Reusability: The data stored in the file can be accessed, updated, and deleted anywhere and anytime providing high reusability.
  • Portability: Without losing any data, files can be transferred to another in the computer system. The risk of flawed coding is minimized with this feature.
  • Efficient: A large amount of input may be required for some programs. File handling allows you to easily access a part of a file using few instructions which saves a lot of time and reduces the chance of errors.
  • Storage Capacity: Files allow you to store a large amount of data without having to worry about storing everything simultaneously in a program.

Types of Files in C

A file can be classified into two types based on the way the file stores the data. They are as follows:

  • Text Files
  • Binary Files

1. Text Files

A text file contains data in the form of ASCII characters and is generally used to store a stream of characters.

  • Each line in a text file ends with a new line character (‘\n’).
  • It can be read or written by any text editor.
  • They are generally stored with .txt file extension.
  • Text files can also be used to store the source code.

2. Binary Files

A binary file contains data in binary form (i.e. 0's and 1's) instead of ASCII characters. They contain data that is stored in a similar manner to how it is stored in the main memory.

  • The binary files can be created only from within a program and their contents can only be read by a program.
  • More secure as they are not easily readable.
  • They are generally stored with .bin file extension.

C File Operations

C file operations refer to the different possible operations that we can perform on a file in C such as:

  1. Creating a new file – fopen() with attributes as "a" or "a+" or "w" or "w+"
  2. Opening an existing file – fopen()
  3. Reading from file – fscanf() or fgets()
  4. Writing to a file – fprintf() or fputs()
  5. Moving to a specific location in a file – fseek(), rewind()
  6. Closing a file – fclose()

File Pointer in C

A file pointer is a reference to a particular position in the opened file. It is used in file handling to perform all file operations such as read, write, close, etc. We use the FILE macro to declare the file pointer variable. The FILE macro is defined inside <stdio.h> header file.

Syntax of File Pointer

FILE* pointer_name;


Open a File in C

For opening a file in C, the fopen() function is used with the filename or file path along with the required access modes.

Syntax of fopen()

FILE* fopen(const char *file_name, const char *access_mode);







Create a File in C

The fopen() function can not only open a file but also can create a file if it does not exist already. For that, we have to use the modes that allow the creation of a file if not found such as w, w+, wb, wb+, a, a+, ab, and ab+.

FILE *fptr;
fptr = fopen("filename.txt", "w");

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:

Example:

FILE * fptr; 
fptr = fopen("fileName.txt”, "r");
fscanf(fptr, "%s %s %s %d", str1, str2, str3, &year);
char c = fgetc(fptr);

Write to a File

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

Example:

FILE *fptr ; 
fptr = fopen("fileName.txt", "w");
fprintf(fptr, "%s %s %s %d", "We", "are", "in", 2012);
fputc("a", fptr);

Closing a File

The fclose() function is used to close the file. After successful file operations, you must always close a file to remove it from the memory.

Syntax of fclose()

fclose(file_pointer);

Example:
FILE *fptr ;
fptr= fopen("fileName.txt", "w");
---------- Some file Operations -------
fclose(fptr);







Tags

Post a Comment

0Comments

Post a Comment (0)