fprintf() fscanf() in C
×


fprintf() fscanf() in C

39

fprintf() in C 

Description:

 The fprintf() function in C is used for formatted output to a stream.

 It writes formatted data to the specified output stream according to the format string format.

Return Value:

 On success, fprintf() returns the number of characters written to the stream.

 On failure, it returns a negative value.

Syntax of fprinf() in C:

int fprintf(FILE *stream, const char *format, ...);

 stream: Pointer to the FILE object that identifies the stream where the output is written.

 format: Format string that specifies the format of the output.

 ...: Additional parameters that match the format specifiers in the format string.

Example:

#include<stdio.h>

int main() {
 FILE *fp;
 int num1 = 42;
 float num2 = 3.14;
 char str[] = "Hello, World!";
 
 // Open a file named "output.txt" in write mode
 fp = fopen("output.txt", "w");
 
 // Check if the file opened successfully
 if (fp == NULL) {
 printf("Error opening file.\n");
 return 1; // Return with error code
 }
 
 // Write formatted output to the file using fprintf()
 fprintf(fp, "Integer: %d\n", num1);
 fprintf(fp, "Float: %.2f\n", num2);
 fprintf(fp, "String: %s\n", str);
 
 // Close the file
 fclose(fp);
 
 printf("Data written to file successfully.\n");
 
 return 0;
}

Output:

Error opening file.

In this example:

 We declare a FILE pointer fp.

 We define an integer num1, a float num2, and a string str

 We create a file named "output.txt" in write mode using fopen().

 We check if the file opened successfully.

 We use fprintf() to write formatted data to the file.

 Finally, we close the file using fclose().

fscanf() in C 

Description:

 The fscanf() function in C is used for formatted input from a stream.

 It reads formatted data from the specified input stream according to the format string format.

Return Value:

 On success, fscanf() returns the number of items successfully read.

 On reaching the end-of-file, it returns EOF.

 On failure, it returns a value less than the number of items to be read.

Syntax of fscanf in C:

int fscanf(FILE *stream, const char *format, ...);

 stream: Pointer to the FILE object that identifies the stream from which input is read.

 format: Format string that specifies the format of the input.

 ...: Pointers to the variables where the read values should be stored.

Example:

#include<stdio.h>

int main() {
 FILE *fp;
 int num1;
 float num2;
 char str[50];
 
 // Open a file named "input.txt" in read mode
 fp = fopen("input.txt", "r");
 
 // Check if the file opened successfully
 if (fp == NULL) {
 printf("Error opening file.\n");
 return 1; // Return with error code
 }
 
 // Read formatted data from the file using fscanf()
 if (fscanf(fp, "%d", &num1) != 1) {
 printf("Error reading integer.\n");
 fclose(fp);
 return 1; // Return with error code
 }

 if (fscanf(fp, "%f", &num2) != 1) {
 printf("Error reading float.\n");
 fclose(fp);
 return 1; // Return with error code
 }

 if (fscanf(fp, "%s", str) != 1) {
 printf("Error reading string.\n");
 fclose(fp);
 return 1; // Return with error code
 }
 
 // Close the file
 fclose(fp);
 
 // Print the read data
 printf("Integer: %d\n", num1);
 printf("Float: %.2f\n", num2);
 printf("String: %s\n", str);
 
 return 0;
}

Output:

Error opening file.

In this example:

 We declare an integer variable num1, a float variable num2, and a character array str.

 We then open a file named "output.txt" in write mode with fopen().

 We open a file named "input.txt" in read mode using fopen().

 We check if the file opened successfully.

 We use fscanf() to read formatted data from the file.

 We check the return value of fscanf() to handle any read errors.

 Finally, we close the file using fclose() and print the read data.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments