fprintf() and fscanf() in C Programming
Hello Everyone, In this article, we will discuss about two important function fprintf() and fscanf() in C Programming. These functions are used for formatted input and output, allowing you to read from and write to files in a structured way. Understanding how to use fprintf() and fscanf() can help you handle file operations more efficiently in your C programs. Whether you are new to C or have some experience, this guide will help you to understand basics of fprintf and fscanf in C effectively with examples. Let’s get started.
Definition:
fprintf() and fscanf() are functions in the C programming language that allow you to read from and write to files.
Syntax
The syntax for fprintf() is:
int fprintf(FILE *stream, const char *format, ...);
The syntax for fscanf() is:
int fscanf(FILE *stream, const char *format, ...)
Here, “stream” is a pointer to the file you want to read from or write to, “format” is a string that specifies how the data should be formatted, and the ellipsis (“…”) represents one or more arguments that match the format string.
Here’s an example of how to use fprintf() and fscanf() to write and read data to and from a file, respectively:
#include <stdio.h>
int main() {
FILE *fp;
int num;
// open the file for writing
fp = fopen("numbers.txt", "w");
// write some numbers to the file
fprintf(fp, "%d %d %d %d", 1, 2, 3, 4);
// close the file
fclose(fp);
// open the file for reading
fp = fopen("numbers.txt", "r");
// read the numbers from the file
fscanf(fp, "%d %d %d %d", &num, &num, &num, &num);
printf("Numbers read from file: %d %d %d %d\\n", num, num, num, num);
// close the file
fclose(fp);
return 0;
}
In this example, we first open a file called “numbers.txt” for writing using the fopen() function. Then, we use the fprintf() function to write the numbers 1, 2, 3, and 4 to the file in the format “%d %d %d %d”.
After closing the file, we open it again for reading using the fopen() function with the “r” mode. We use the fscanf() function to read the numbers from the file in the same format as we wrote them, and store them in the variable “num”. We then print out the numbers to the console using the printf() function.
When you run the above code, you should see the following output:
Numbers read from file: 1 2 3 4
This indicates that we were able to successfully write the numbers to the file using fprintf(), and then read them back using fscanf().
#include <stdio.h>
int main() {
FILE *fp;
int numExpenses;
float expenseAmount, totalExpenses = 0;
// ask user for number of expenses
printf("How many expenses did you have this month?\\n");
scanf("%d", &numExpenses);
// open file for writing
fp = fopen("expenses.txt", "w");
// write expenses to file
fprintf(fp, "Expenses:\\n");
for (int i = 0; i < numExpenses; i++) {
printf("Enter expense #%d amount: $", i + 1);
scanf("%f", &expenseAmount);
fprintf(fp, "- $%.2f\\n", expenseAmount);
totalExpenses += expenseAmount;
}
// write total expenses to file
fprintf(fp, "Total expenses: $%.2f\\n", totalExpenses);
// close file
fclose(fp);
// open file for reading
fp = fopen("expenses.txt", "r");
// read and print expenses from file
char line[100];
printf("\\nExpenses:\\n");
while (fgets(line, sizeof(line), fp)) {
printf("%s", line);
}
// close file
fclose(fp);
return 0;
}
In this program, we start by asking the user for the number of expenses they had that month. We then open a file called “expenses.txt” for writing, and use a for loop to ask the user for each expense amount, write it to the file using fprintf(), and keep a running total of the expenses.
Once we have written all the expenses to the file, we write the total expenses to the file as well using fprintf(). We then close the file.
Next, we open the file for reading, and use a while loop with fgets() to read each line of the file and print it to the console. Finally, we close the file.
When the program is run, it will prompt the user to enter the number of expenses they had that month, and then ask them to enter the amount for each expense. It will write these expenses to the file, along with the total expenses. Finally, it will read and print the expenses from the file to the console.
For example, if the user entered that they had 3 expenses of $10, $20, and $30, the output would look like this:
How many expenses did you have this month?
3
Enter expense #1 amount: $10
Enter expense #2 amount: $20
Enter expense #3 amount: $30
Expenses:
- $10.00
- $20.00
- $30.00
Total expenses: $60.00