Professional Documents
Culture Documents
Steps To Process A File
Steps To Process A File
Open a file
1.Use fopen function
fopen(filename,mode);
2.Assign it to the pointer
Pointer_name=fopen(filename,mode);
Fp=fopen(emp.txt,r);
Example
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
fp=fopen("student.txt","r");
if(fp==NULL)
{
printf("File could not be opened");
}
getch();
}
Or
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char fname[20];
printf("Enter file name");
gets(fname);
fp=fopen(fname,"w");
if(fp==NULL)
{
printf("File could not be opened");
}
getch();
}
File Modes
Example:fprintf()
scanf("%d",&empid);
printf("Enter your salary");
scanf("%f",&salary);
fprintf(fp,"%s\t%d\t%f\t",name,empid,salary);
fflush(stdin);
}
fclose(fp);
getch();
}
Example:fscanf()
getch();
}