C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Formatting Read And Write Files (fscanf() And fprintf() Functions) in C Programming Language

fscanf and fprintf are standard library functions in C used to read formatted data from and write formatted data to files, respectively. These functions are similar to scanf and printf, but they operate on files rather than standard input and output. This tutorial will explain how to use fscanf and fprintf for file input and output in C.

  • Using fscanf

The fscanf function reads formatted data from a file, and its syntax is as follows:

int fscanf(FILE *stream, const char *format, ...);
  • stream: A pointer to a FILE object representing the input file.
  • format: A format string specifying the expected input format (similar to scanf).
  • ...: A list of pointers to variables that will store the read values.

Here's an example of reading formatted data from a file using fscanf:

#include <stdio.h>

int main() {
    FILE *file = fopen("input.txt", "r");
    if (file == NULL) {
        printf("Error opening file for reading.\n");
        return 1;
    }

    int num1, num2;
    fscanf(file, "%d %d", &num1, &num2);
    printf("Read numbers: %d, %d\n", num1, num2);

    fclose(file);
    return 0;
}

In this example, the program opens the file "input.txt" for reading, reads two integer values from it using fscanf, and prints the read values.

  • Using fprintf

The fprintf function writes formatted data to a file, and its syntax is as follows:

int fprintf(FILE *stream, const char *format, ...);
  • stream: A pointer to a FILE object representing the output file.
  • format: A format string specifying the output format (similar to printf).
  • ...: A list of values to be written to the file.

Here's an example of writing formatted data to a file using fprintf:

#include <stdio.h>

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file == NULL) {
        printf("Error opening file for writing.\n");
        return 1;
    }

    int num1 = 42, num2 = 89;
    fprintf(file, "The numbers are %d and %d\n", num1, num2);

    fclose(file);
    return 0;
}

In this example, the program opens the file "output.txt" for writing, writes a formatted string with two integer values to it using fprintf, and closes the file.

In summary, fscanf and fprintf are C standard library functions used to read formatted data from and write formatted data to files. They are similar to scanf and printf but operate on files rather than standard input and output. To use fscanf and fprintf, you need a pointer to a FILE object, a format string, and a list of variables (for fscanf) or values (for fprintf). These functions provide a convenient way to read and write formatted data in files in C programming.

  1. Reading Formatted Data from Files using fscanf in C:

    #include <stdio.h>
    
    int main() {
        FILE *file = fopen("input.txt", "r");
    
        if (file != NULL) {
            int num;
            char str[50];
    
            // Reading formatted data from file
            fscanf(file, "%d %s", &num, str);
    
            printf("Read: %d, %s\n", num, str);
    
            fclose(file);
        } else {
            perror("Error opening file for reading");
        }
    
        return 0;
    }
    
  2. Writing Formatted Data to Files with fprintf in C:

    #include <stdio.h>
    
    int main() {
        FILE *file = fopen("output.txt", "w");
    
        if (file != NULL) {
            int num = 42;
            char str[] = "Hello, File!";
    
            // Writing formatted data to file
            fprintf(file, "%d %s", num, str);
    
            fclose(file);
        } else {
            perror("Error opening file for writing");
        }
    
        return 0;
    }
    
  3. Formatting Options for fscanf in C:

    #include <stdio.h>
    
    int main() {
        FILE *file = fopen("input.txt", "r");
    
        if (file != NULL) {
            int num1, num2;
    
            // Reading formatted data with specific format options
            fscanf(file, "%4d %*c %3d", &num1, &num2);
    
            printf("Read: %d, %d\n", num1, num2);
    
            fclose(file);
        } else {
            perror("Error opening file for reading");
        }
    
        return 0;
    }
    
  4. C Code Examples with fscanf and fprintf Functions:

    #include <stdio.h>
    
    int main() {
        FILE *inputFile = fopen("input.txt", "r");
        FILE *outputFile = fopen("output.txt", "w");
    
        if (inputFile != NULL && outputFile != NULL) {
            int num;
            char str[50];
    
            // Reading formatted data from input file
            fscanf(inputFile, "%d %s", &num, str);
    
            // Writing formatted data to output file
            fprintf(outputFile, "Read: %d, %s", num, str);
    
            fclose(inputFile);
            fclose(outputFile);
        } else {
            perror("Error opening files");
        }
    
        return 0;
    }
    
  5. Specifiers and Flags in fprintf for File Output in C:

    #include <stdio.h>
    
    int main() {
        FILE *file = fopen("output.txt", "w");
    
        if (file != NULL) {
            int num = 42;
    
            // Specifiers and flags in fprintf for file output
            fprintf(file, "%05d", num);  // Pad with zeros to width 5
    
            fclose(file);
        } else {
            perror("Error opening file for writing");
        }
    
        return 0;
    }
    
  6. Error Handling with fscanf and fprintf in C:

    #include <stdio.h>
    
    int main() {
        FILE *file = fopen("input.txt", "r");
    
        if (file != NULL) {
            int num;
    
            // Error handling with fscanf
            if (fscanf(file, "%d", &num) == 1) {
                printf("Read: %d\n", num);
            } else {
                printf("Error reading from file\n");
            }
    
            fclose(file);
        } else {
            perror("Error opening file for reading");
        }
    
        return 0;
    }
    
  7. Formatting Numeric and Character Data in C Files:

    #include <stdio.h>
    
    int main() {
        FILE *file = fopen("output.txt", "w");
    
        if (file != NULL) {
            int num = 42;
            char ch = 'A';
    
            // Formatting numeric and character data in file
            fprintf(file, "Number: %d\nCharacter: %c\n", num, ch);
    
            fclose(file);
        } else {
            perror("Error opening file for writing");
        }
    
        return 0;
    }
    
  8. Sequential File Processing with fscanf and fprintf:

    #include <stdio.h>
    
    int main() {
        FILE *inputFile = fopen("input.txt", "r");
        FILE *outputFile = fopen("output.txt", "w");
    
        if (inputFile != NULL && outputFile != NULL) {
            int num;
            char str[50];
    
            // Sequential file processing
            while (fscanf(inputFile, "%d %s", &num, str) == 2) {
                fprintf(outputFile, "Read: %d, %s\n", num, str);
            }
    
            fclose(inputFile);
            fclose(outputFile);
        } else {
            perror("Error opening files");
        }
    
        return 0;
    }