fprintf() & fscanf() in C programming
File Handling: fprintf() & fscanf() in C programming
FIle handling is an important feature in while storing data in device. It is done with the help of C programming. It helps in curbing with the problem of displaying large amount of data due to some console limitations. In this section we would talk about fprintf() & fscanf() in C programming.
fprintf() & fscanf() in C programming
fprintf()- It is used to write content or string into a file instead of stdout console.
fscanf()- It is used to read content or string from a file pointed by a file pointer into the stream.
Syntax of fprintf() :
int fprintf(FILE *stream, const char *string, ...);
There are various parts of this syntax, they are mentioned below:
- Stream- It is used to point to the File object that helps to find the stream in which data is to be written.
- Format- It is used to write a string into the file pointed by the stream pointer. It can use embedded format tags that can be replaced by the values mentioned in subsequently added arguments.
Example:
Run
#include<stdio.h>
int main (void)
{
FILE *new;
new = fopen("sample.txt","w");
fprintf(fileName, "%s %s %d", "Welcome", "to", 2023);
fclose(New);
return 0;
}
Output:
The file sample text is open and contains "Welcome to 2023" in its file.
Syntax of fscanf() :
int fscanf(FILE *stream, const char *format, ...)
There are various part of the fscanf() syntax, they are mentioned below:
- Stream- Its is used to as the file pointer to point where the output will end.
- Format- Its is used as a string that contains various Format arguments.
Example:
Run
#include<stdio.h>
main(){
FILE *new;
char b[255];
new = fopen("sample.txt", "r");
while(fscanf(new, "%s", b)!=EOF){
printf("%s ", b );
}
fclose(new);
}
Output:
Welcome to 2023
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment