file handling in c
file handling using c programming:-
example 1 :- write to a text file-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main() {
FILE *fp;
int num;
fp=fopen("files1.1.txt","w");
if(fp==NULL) {
printf(" file does not exit");
exit(1);
}
printf("enter the num:");
scanf("%d",&num);
fprintf(fp,"%d",num);
printf("\n value of n is :%d",num);
fclose(fp);
return 0;
}
example 2:- read to a text file-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main() {
FILE *fp;
int num;
//if((fp=fopen("files1.1.txt","r"))=NULL)
fp=fopen("files1.1.txt","r");
if(fp==NULL) {
printf(" file does not exit");
exit(1);
}
fscanf(fp,"%d",&num);
printf("\n value of n=%d",num);
fclose(fp);
return 0;
}
example 3:- in file wite a character-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("f1.txt","a");
if(fp==NULL)
{
printf("file does not exist!!!");
exit(1);
}
printf("enter the character:");
scanf("%c",&ch);
fprintf(fp,"%c",ch);
printf("\n ur character is %c",ch);
return 0;
}
example 4:- in file read a character-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("f1.txt","r");
if(fp==NULL)
{
printf("file does not exist!!!");
exit(1);
}
fscanf(fp,"%c",&ch);
printf("\n ur character is: %c",ch);
fclose(fp);
return 0;
}
Comments
Post a Comment
hey