File Handling Operation In C Language
CSZONE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *f;
int insert();
int read();
int count();
int main(){
int ch;
system("clear");
printf("\n\t\tFile Handling");
printf("\n\t\tMenu");
printf("\n\t\t1.Write ");
printf("\n\t\t2.Read");
printf("\n\t\t3 Count ");
printf("\n\t\tEnter Your Choice : ");
scanf("%d",&ch);
switch(ch){
case 1 : insert();
break;
case 2 : read();
break;
case 3 : count();
break;
default: printf("\n\t\tYou Have Entered Wrong Choice.. Try Again...");
exit(0);
}
return 0;
}
int insert(){
char ch[500];
f = fopen("File.c","w");
if(f==NULL){
printf("\n\t\tFile Opening Error..");
exit(0);
}else{
printf("\n\t\tEnter Data To File : ");
scanf("%s",ch);
fprintf(f,"%s",ch);
printf("\n\t\tData Successfully Inserted Into File");
}
fclose(f);
}
int read(){
char ch;
f = fopen("File.c","r");
if(f==NULL){
printf("\n\t\tFile Opening Error");
exit(0);
}else{
printf("\n\t\tData of File : ");
while(ch!=EOF){
ch = fgetc(f);
printf("%c",ch);
}
}
fclose(f);
}
int count(){
char ch;
int c=-1;
f = fopen("File.c","r");
if(f==NULL){
printf("\n\t\tFile Opening Error");
exit(0);
}else{
printf("\n\t\tData of File : ");
while(ch!=EOF){
ch = fgetc(f);
printf("%c",ch);
c++;
}
}
printf("\n\t\tTotal : %d",c);
fclose(f);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *f;
int insert();
int read();
int count();
int main(){
int ch;
system("clear");
printf("\n\t\tFile Handling");
printf("\n\t\tMenu");
printf("\n\t\t1.Write ");
printf("\n\t\t2.Read");
printf("\n\t\t3 Count ");
printf("\n\t\tEnter Your Choice : ");
scanf("%d",&ch);
switch(ch){
case 1 : insert();
break;
case 2 : read();
break;
case 3 : count();
break;
default: printf("\n\t\tYou Have Entered Wrong Choice.. Try Again...");
exit(0);
}
return 0;
}
int insert(){
char ch[500];
f = fopen("File.c","w");
if(f==NULL){
printf("\n\t\tFile Opening Error..");
exit(0);
}else{
printf("\n\t\tEnter Data To File : ");
scanf("%s",ch);
fprintf(f,"%s",ch);
printf("\n\t\tData Successfully Inserted Into File");
}
fclose(f);
}
int read(){
char ch;
f = fopen("File.c","r");
if(f==NULL){
printf("\n\t\tFile Opening Error");
exit(0);
}else{
printf("\n\t\tData of File : ");
while(ch!=EOF){
ch = fgetc(f);
printf("%c",ch);
}
}
fclose(f);
}
int count(){
char ch;
int c=-1;
f = fopen("File.c","r");
if(f==NULL){
printf("\n\t\tFile Opening Error");
exit(0);
}else{
printf("\n\t\tData of File : ");
while(ch!=EOF){
ch = fgetc(f);
printf("%c",ch);
c++;
}
}
printf("\n\t\tTotal : %d",c);
fclose(f);
}
Comments
Post a Comment