File Handling programs
|
#include<stdio.h> int main( ) { FILE *fptr; char *text; fptr = fopen("prowess.txt","w"); if(fptr != NULL) { printf("Enter text here: "); gets(text); for(i=0; text[i]!='\0'; i++) { putc(text[i],fptr); } printf("Data Saved!!"); return 0; }OUTPUT :
Enter text here : C is a simple language Data Saved!!
|
#include<stdio.h> int main( ) { FILE *fptr; char ch; fptr = fopen("prowess.txt","r"); if(fptr != NULL) { do{ ch = fgetc(fptr); printf("%c",ch); } while(ch!=EOF); return 0; }OUTPUT :
C is a Simple language
|
#include<stdio.h> int main( ) { FILE *fptr; char name[20]; int age; float salary; fptr = fopen("emp.txt", "a+"); if (fptr != NULL) { printf("Enter the name : "); scanf("%s", name); fprintf(fptr,"%s\n", name); printf("Enter the age : "); scanf("%d", &age); fprintf(fptr,"%d\n", age); printf("Enter the salary : "); scanf("%f", &salary); fprintf(fptr,"f\n", salary); fclose(fptr); printf("Record saved in File"); } return 0; }OUTPUT :
Enter the name : Deepak Enter the age : 24 Enter the salary : 30000.00 Record saved in File
|
#include<stdio.h> int main( ) { FILE *fptr; char ch; int charIndex; fptr = fopen("prowess.txt","r"); if(fptr != NULL) { fseek(fptr,-1,SEEK_END); charIndex=ftell(fptr); do { ch = fgetc(fptr); printf("%c",ch); fseek(fptr,-2,SEEK_CUR); charIndex--; } while(charIndex!=-1); return 0; }OUTPUT :
egaugnal elpmiS a si C
|
#include<stdio.h> int main( ) { FILE *fp1, *fp2; fp1 = fopen("data.txt", "r"); fp2 = fopen("data_cpy.txt", "w"); if (fptr1 != NULL) { ch = fgetc(fp1); while (ch != EOF) { fputc(ch, fp2); ch = fgetc(fp1); } printf("File Copied."); } printf("Files merged successfully."); fclose(fp1); fclose(fp2); return 0; }OUTPUT :
File Copied.
|
#include<stdio.h> int main( ) { FILE *fptr; char ch; int c=0, total=0; fptr = fopen("prowess.txt","r"); if(fptr != NULL) { ch = fgetc(fptr); while(ch!=EOF){ if(ch==' '){ c++; } else if(ch=='\n'){ total=total+c+1; c=0; } ch = fgetc(fptr); } printf("TOTAl WORDS: %d",total); return 0; }OUTPUT :
FILE CONTENT IS: C is a simple language. TOTAL WORDS: 5