Definition: fgets()
is a standard C library function used for reading a line of text from a file.
Syntax:
fgets(string_variable, int value,file_pointer);
Example:
WAP to open the file “abc.txt” , read its contents and display them to the screen.(using fgets() function)
#include<stdio.h>
int main()
{
FILE *fptr;
char str[200];
fptr = fopen("abc.txt", "r");
if(fptr==NULL)
{
printf("file cannot be open\n");
}
fgets(str, 100, fptr);
printf("%s", str);
fclose(fptr);
}
2. fputs()
Definition: fputs()
is a standard C library function used for writing a string to a file.
Syntax:
fputs(str_variable, file_pointer);
Example:
WAP to create a file named “abc.txt” and write a string to this file using fputs() function.
#include<stdio.h>
int main()
{
FILE *fptr;
char str[200];
fptr = fopen("abc.txt", "w");
if(fptr==NULL)
{
printf("file cannot be open\n");
}
printf("enter the string you want to write in file");
gets(str); // read string that you want to write in file from user
fputs(str, fptr);
fclose(fptr);
}