prowessapps.in File Handling programs

 C Program to write the data in File.

#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!!

 C Program to read content from File.

#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

 C Program to create a file called emp.txt and store information about a person, in terms of his name, age and salary..

#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

 C Program to read file and print content in reverse order.

#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

 C Program to copy the content of file into another file.

#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.

 C Program to count the words in a file.

#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


 C Program to List Files in current Directory.

#include<dirent.h>
#include<stdio.h> 
int main( ) {
 DIR *d;
 struct dirent *dir;
 d = opendir(".");
 if (d) {
  while ((dir = readdir(d)) != NULL) {
   printf("%s\n", dir->d_name);
  }
  closedir(d);
  }
 ret
OUTPUT :
first.txt
prowess.txt
second.txt


Get it on Google Play


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved