C File Operations: Handling Text and Binary Files

Working with Files in C

The C language provides robust capabilities for creating, opening, and manipulating both text and binary files. This guide covers the essential file operations you need to know.

1. Opening Files

Before performing any file operations, you need to initialize a FILE object pointer. The fopen() function serves this purpose, taking a filename and mode as parameters.

Mode Description
"r" Opens an existing text file for reading only.
"w" Creates a new text file for writing. If the file exists, its contents are truncated.
"a" Opens a text file for appending. Creates a new file if it doesn't exist.
"r+" Opens a text file for both reading and writing.
"w+" Creates a new text file for reading and writing. Truncates existing files.
"a+" Opens a text file for reading and appending. Reading starts from the beginning.

To work with binary files, simply append 'b' to any of these modes (e.g., "rb", "wb", "ab").

FILE *file_descriptor = NULL;
file_descriptor = fopen("data.txt", "r"); // Using relative path

// For binary files:
FILE *binary_file = fopen("data.bin", "rb");

2. Closing Files

After completing file operations, it's crucial to close the file using fclose(). This releases system resources and ensures all buffered data is written to the file.

fclose(file_descriptor);

3. Writting to Files

When writing to files, these functions return an error indicator (EOF) on failure, allowing you to verify successful operations.

FILE *output_file = NULL;
// Creates/opens test.txt in the system temp directory
output_file = fopen("/tmp/test.txt", "w+");

// Method 1: Formatted output
fprintf(output_file, "This is a test of fprintf functionality...\n");

// Method 2: String output
fputs("This demonstrates fputs operation...\n", output_file);

4. Reading from Files

C provides several methods for reading file content:

  • fgetc(FILE *file_ptr): Reads a single character from the file and returns it as an int. Returns EOF on error or end of file.
  • fgets(char *buffer, int size, FILE *file_ptr): Reads up to size-1 characters from the file into the buffer, appending a null terminator. Returns NULL on error or end of file.
  • fscanf(FILE *file_ptr, const char *format, ...): Reads formatted input from the file according to the specified format string.
#include <stdio.h>
int main() {
    FILE *input_file = NULL;
    char buffer[255];
    
    input_file = fopen("/tmp/test.txt", "r");
    
    // Read first word with fscanf
    fscanf(input_file, "%s", buffer);
    printf("1: %s\n", buffer);  // Output: 1: This
    
    // Read next line with fgets
    fgets(buffer, 255, input_file);
    printf("2: %s\n", buffer);  // Output: 2: is a test of fprintf functionality...
    
    // Read remaining content
    fgets(buffer, 255, input_file);
    printf("3: %s\n", buffer);  // Output: 3: This demonstrates fputs operation...
    
    fclose(input_file);
    return 0;
}</stdio.h>

5. Positioning File Pointers

The fseek() function allows you to move the file pointer to a specific position within the file:

int fseek(FILE *file_stream, long offset, int origin);

The offset parameter specifeis the number of bytes to move from the origin position:

  • Positive offset: Move forward
  • Negative offset: Move backward

The origin parameter can be:

  • SEEK_SET: Beginning of the file
  • SEEK_CUR: Current position
  • SEEK_END: End of the file

Tags: c programming file operations text files Binary Files File I/O

Posted on Wed, 03 Jun 2026 18:04:13 +0000 by modulor