File Handling in C
File Handling in C is used for permanent storage of data. A file represents a sequence of bytes on the disk where a group of related data is stored. It is used to open, store, read, search or close files.
What is File in C ?
A collection of data or information is known as file. A file is a collection of bytes on secondary storage
device such as hard disk .
Example of File Handling in C
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fptr;
int roll_no;
char stu_name[50];
fptr = fopen("student.txt", "w+");/* open for writing */
//student .txt is a file in which is used for store the data
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the Rollno\n");
scanf("%d", &roll_no);
fprintf(fptr, "Rollno= %d\n", roll_no);
printf("Enter the name \n");
scanf("%s", stu_name);
fprintf(fptr, "Name= %s\n", stu_name);
fclose(fptr);
}
Output
Enter Rollno 11 Enter the name Faiz
When we open the student.txt file then it contains-
Rollno = 11
Name = Faiz
No comments :
Post a Comment