gets function in c | fgets function in c | gets in c programming
In this article gets function in c we give the information about file handling function in C such as fgets c, fprintf, fscanf in c and fputs in c, gets in c, fgets function in c, gets in c programming.
fputc() – File Handling
Data is written to the file with the fputc() function.
Syntax for fputc()
fputc(character, file_pointer);
- Character: The character which is to be written in the file, that character comes here.
- file_pointer: The pointer of the file to which the file is to be written.
// Using fputc()function program in File handling.
#include <stdio.h>
int main()
{
char ch;
FILE *ptr;
ptr=fopen(“C:\\Demo1.txt”,”w”);
if(ptr==NULL)
{
printf(“File is not opened or created”);
}
else
{
printf(“Enter Single Character : “);
scanf(“%c”,&ch);
fputc(ch,ptr);
fclose(ptr);
}
return 0;
}
OUTPUT:-
Enter Single Character: S
fputs() function Syntax:
fputs(string_value, file_pointer);
#include <stdio.h>
int main()
{
char name[20];
FILE *ptr;
ptr=fopen(“C:\\Demo2.txt”,”w”);
if(ptr==NULL)
{
printf(“File is not opened or created”);
}
else
{
printf(“Enter Your Name : “);
scanf(“%s”,name);
fputs(name,ptr);
fclose(ptr);
}
return 0;
}
Output:
Enter Your Name: Yashraj
fgetc() – File Handling
The data in the file is read with the fgetc() function.
Syntax for fgetc()
fgetc(file_pointer);
- file_pointer: The pointer of the file to which the file is to be read.
- It normally returns only a single character from the file. but due to feof (end of file), it keeps on printing one-by-one characters until the characters in the file are exhausted.
#include <stdio.h>
int main()
{
char ch;
FILE *ptr;
ptr=fopen(“C:\\Demo2.txt”,”r”);
if(ptr==NULL)
{
printf(“File is not opened”);
}
else
{
while(!feof(ptr))
{
ch=getc(ptr);
printf(“%c”,ch);
}
fclose(ptr);
}
return 0;
}
Output:
11 Amol 19.700000
12 Raj 18.900000
13 Geeta 19.300000
#include <stdio.h>
int main()
{
char ch;
FILE *ptr;
ptr=fopen(“Demo.txt”,”r”);
if(ptr==NULL)
{
printf(“File is not opened”);
}
else
{
while((ch=fgetc(ptr))!=EOF)
{
printf(“%c”,ch);
}
fclose(ptr);
}
return 0;
}
Output:
11 Amol 9.700000
12 Raj 8.900000
13 Geeta 9.300000
fprintf() – File Handling
Data is written to the file with the fprintf() function.
Syntax for fprintf()
fprintf(file_pointer, “format_specifier or string”, variables);
- file_pointer: The pointer of the file to which the file is to be written.
- format specifier or string: as used in printf, similarly format specifier and string are used here. Both can also be used together.
- Variables : If the format specifier is given, it is a variable. If there were no format specifiers, there would be no variables.
#include <stdio.h>
int main()
{
char name[20];
int roll;
float cgpa;
FILE *ptr;
ptr=fopen(“C:\\Demo3.txt”,”w”);
if(ptr==NULL)
{
printf(“File is not opened or created”);
}
else
{
printf(“Enter Student Roll No. : “);
scanf(“%d”,&roll);
printf(“Enter Student Name : “);
scanf(“%s”,name);
printf(“Enter Student CGPA : “);
scanf(“%f”,&cgpa);
fprintf(ptr,”Roll : %d, Name : %s, CGPA : %f”,roll,name,cgpa);
fclose(ptr);
} return 0;
}
Output:
1.Enter Student Roll No. : 12
2.Enter Student Name : Raj
3.Enter Student CGPA : 8.9
fscanf() – File Handling
This function is used to read the data in the file.
Syntax for fscanf()
fscanf(file_pointer, “format_specifier or string”, &variables);
- file_pointer: The pointer of the file to which the file is to be read.
- format specifier or string: as used in scanf, similarly format specifier and string are used here. Both can also be used together.
- Variables : If the format specifier is given, it is a variable. If there were no format specifiers, there would be no variables.
#include <stdio.h>
int main()
{
char name[20];
int roll,i,n;
float cgpa;
FILE *ptr;
ptr=fopen(“C:\\Demo3.txt”,”r”);
if(ptr==NULL)
{
printf(“File is not opened”);
}
else
{
while(!feof(ptr))
{
fscanf(ptr,”%d%s%f”,&roll,name,&cgpa);
printf(“Roll : %d, Name : %s, CGPA : %f\n”,roll,name,cgpa);
}
fclose(ptr);
}
return 0;
}
Output:
1.Roll : 11, Name : Amol, CGPA : 8.900000
2.Roll : 12, Name : Raj, CGPA : 9.400000
3.Roll : 13, Name : Geeta, CGPA : 8.500000
Related Link:
- Object Oriented Programming Using C++ |BCA Semester II |History of C++
- Core Java Programming | BCA Part III | SEM-VI | What is Java
- Computer Fundamentals Notes | Computer Fundamentals Tutorial
- DOT NET Technology Tutorial | ASP.NET Tutorial for Beginners
Good article. I definitely appreciate this website. Stick with it!