Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Tuesday, August 12, 2014

Program to copy a file

C program to copy file. If you have any doubts, let me know

#include <stdlib.h>
#include <stdio.h>
main()
{
   
FILE *source, *target;
char x, sourcef[100], targetf[100];
printf("File to copy?"); gets(sourcef);   source=fopen(sourcef, "r");  printf("Target File?"); gets(targetf);   target=fopen(targetf, "w");   while((x=fgetc(source))!=EOF) fputc(x, target);   printf("The file has been copied."); }

Program to read from file

Program to read from a file. If you have any doubts, let me know

#include <stdlib.h>
#include <stdio.h>
main()
{
char x, name[100];
FILE *pf;
    printf("Name of file?");
    gets(name);
    pf = fopen(name,"r");
    printf("Contents are :");
         while((x=fgetc(pf))!= EOF)
         printf("%c",x);
    }

Saturday, August 9, 2014

Program to write to a file and display the vowel which occurred the most

Program to write to a file and display the vowel which occurred the most often. If you need help comment below

#include<stdio.h>
main()
{
  FILE *f1;
  char c;
  int a,e,i,o,u;
  a=0;
  e=0;
  i=0;
  u=0;
  o=0;
  printf("\nEnter the letters to the file:\n");
  f1=fopen("TEXT.txt","w");
  while((c=getchar())!='\n')
    putc(c,f1);
  fclose(f1);
  f1=fopen("TEXT.txt","r");
  while((c=getc(f1))!=EOF)
   {
     if(c=='a')
       a++;
     else if(c=='e')
       e++;
     else if(c=='i')
       i++;
     else if(c=='o')
       o++;
     else if(c=='u')
       u++;
   }
  fclose(f1);
  if((a>=e)&&(a>=i)&&(a>=o)&&(a>=u))
   printf("\n\n'a'is the vowel which occured the most.\n%d times",a);
  if((e>=a)&&(e>=i)&&(e>=u)&&(e>=o))
   printf("\n\n'e' is the vowel which occured the most.\n%d times",e);
  if((i>=a)&&(i>=e)&&(i>=o)&&(i>=u))
   printf("\n\n'i' is the vowel which occured the most.\n%d times",i);
  if((o>=a)&&(o>=e)&&(o>=i)&&(o>=u))
   printf("\n\n'o'is the vowel which occured the most.\n%d times",o);
  if((u>=a)&&(u>=e)&&(u>=i)&&(u>=o))
   printf("\n\n'u' is the vowel which occured the most.\n%d times",u);
  printf("\n\n");
}