Operations on Files in C

Working with files in C involves several important operations that allow you to open, read, write, append, and close files. These operations help manage data storage, making it possible to interact with files for persistent data handling. At SamagraCS Educational Technology, we break down the key file operations and provide examples to help you master these concepts.


Key Operations on Files in C

Here are the main operations you can perform on files in C:

  1. Opening a File: Using fopen() to open a file.
  2. Reading from a File: Using fscanf(), fgets(), fread().
  3. Writing to a File: Using fprintf(), fputs(), fwrite().
  4. Appending to a File: Using fopen() in append mode ("a", "a+").
  5. Closing a File: Using fclose().
  6. File Positioning: Using fseek(), ftell(), and rewind() to manage file pointers.
  7. Error Handling: Using functions like feof() and ferror() to detect errors and end-of-file conditions.

1. Opening a File (fopen())

Before performing any operations on a file, you must open the file using the fopen() function. This function returns a file pointer, which you’ll use in subsequent operations like reading or writing.

Syntax:

  • filename: The name of the file you want to open.
  • mode: The mode in which you want to open the file (e.g., "r", "w", "a", "rb", "wb").

Example:


2. Reading from a File

Once a file is open, you can read data from it using functions like fscanf(), fgets(), and fread().

a) fscanf() – Reading Formatted Data

b) fgets() – Reading a String from a File

c) fread() – Reading Binary Data


3. Writing to a File

You can write data to a file using fprintf(), fputs(), or fwrite().

a) fprintf() – Writing Formatted Data

b) fputs() – Writing Strings

c) fwrite() – Writing Binary Data


4. Appending to a File

To add data to the end of an existing file without overwriting its contents, you can open the file in append mode using "a" or "a+".

Example:

In this case, the new line "Added new line." is added to the end of data.txt.


5. Closing a File (fclose())

After finishing any file operation, always close the file using fclose() to free up system resources and ensure all data is written properly.

Syntax:

Example:


6. File Positioning Functions

C provides functions to manage the position of the file pointer, which helps when navigating within the file.

a) fseek() – Move File Pointer

The fseek() function moves the file pointer to a specific position in the file.

  • offset: Number of bytes to move.
  • origin: Position to move from (SEEK_SET, SEEK_CUR, SEEK_END).

Example:


b) ftell() – Get File Pointer Position

The ftell() function returns the current position of the file pointer.

Example:


c) rewind() – Reset File Pointer

The rewind() function sets the file pointer to the beginning of the file.

Example:

rewind(filePointer);  // Move the file pointer to the beginning

7. Error Handling in File I/O

C provides functions like feof() and ferror() to detect the end of the file or any errors during file operations.

a) feof() – Check End of File

The feof() function returns non-zero if the file pointer has reached the end of the file.

Example:

b) ferror() – Check for Errors

The ferror() function checks if any error occurred during file operations.

Example:


Mastering file operations in C is essential for managing data storage and retrieval in applications. By understanding the key operations—opening, reading, writing, appending, closing, file positioning, and error handling—you’ll be able to efficiently handle files in C programming.

At SamagraCS Educational Technology, we encourage you to practice these operations with different file types and scenarios. If you have any questions, feel free to reach out to Pawan & Pooja, or the team. Happy coding!

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?