C fprintf() and fscanf()

C is a powerful programming language that provides a wide range of functions to handle various tasks. Two of the most commonly used functions in C for input/output operations are fprintf() and fscanf(). These functions are part of the C standard library and are used to write and read data respectively.

What is fprintf()?

The fprintf() function is used to write formatted output to a file. It allows you to write data to a file in a specific format, which can be useful for generating reports, logs, or any other type of structured output. The function takes a format string as its first argument, followed by the data to be written. The format string can contain placeholders that are replaced by the actual values during the writing process.

Here’s an example of how to use fprintf() to write data to a file:

#include <stdio.h>

int main() {
   FILE *file;
   file = fopen("output.txt", "w");

   if (file != NULL) {
      int number = 10;
      fprintf(file, "The number is: %d", number);
      fclose(file);
   }

   return 0;
}

In this example, we open a file called “output.txt” in write mode using the fopen() function. If the file is successfully opened, we use fprintf() to write the value of the variable “number” to the file. Finally, we close the file using fclose().

What is fscanf()?

The fscanf() function is used to read formatted input from a file. It allows you to read data from a file in a specific format, which can be useful for parsing structured data. The function takes a format string as its first argument, followed by the variables where the read data will be stored. The format string specifies the expected format of the input data.

Here’s an example of how to use fscanf() to read data from a file:

#include <stdio.h>

int main() {
   FILE *file;
   file = fopen("input.txt", "r");

   if (file != NULL) {
      int number;
      fscanf(file, "%d", &number);
      printf("The number is: %d", number);
      fclose(file);
   }

   return 0;
}

In this example, we open a file called “input.txt” in read mode using the fopen() function. If the file is successfully opened, we use fscanf() to read an integer from the file and store it in the variable “number”. Finally, we print the value of “number” using printf() and close the file using fclose().

Common Format Specifiers

Both fprintf() and fscanf() support a wide range of format specifiers that allow you to format the input and output data according to your needs. Here are some commonly used format specifiers:

  • %d – for integers
  • %f – for floating-point numbers
  • %c – for characters
  • %s – for strings
  • %x – for hexadecimal numbers

These are just a few examples, and there are many more format specifiers available. You can find a complete list of format specifiers in the C documentation.

In summary, fprintf() and fscanf() are powerful functions in C that allow you to write and read formatted data to and from files. They provide a flexible way to handle input/output operations and are widely used in various programming tasks. Understanding how to use these functions can greatly enhance your ability to work with files in C.

Scroll to Top