Showing posts with label File Handling. Show all posts
Showing posts with label File Handling. Show all posts

Dictionary Word Search C Program

How to write a C Program to Find Dictionary Word Search ?


Solution For C Program to Find a Word in Dictionary :

C Program to copy a file to another file

How to write a C Program to copy a file to another file in C Programming Language ?

This C Program to copy a file to another file.

Solution:

  1. #include<stdio.h>
  2. void main()
  3. {
  4.         FILE *fp1,*fp2;
  5.         char ch;
  6.         char fname1[30],fname2[30];
  7.         printf("Enter the source file: ");
  8.         fflush(stdin);
  9.         scanf("%s",fname1);
  10.         printf("Enter the destination file: ");
  11.         fflush(stdin);
  12.         scanf("%s",fname2);
  13.         fp1=fopen(fname1,"r");
  14.         fp2=fopen(fname2,"w");
  15.         if(fp1==NULL)
  16.         {
  17.                 printf("\n cannot open file %s for reading",fname1);
  18.                 exit(1);
  19.         }
  20.         else if(fp2==NULL)
  21.         {
  22.                 printf("\n cannot open file %s for writing",fname2);
  23.                 exit(1);
  24.         }
  25.         else
  26.         {
  27.                 ch=getc(fp1);
  28.         while(ch!=EOF)
  29.         {
  30.                 putc(ch,fp2);
  31.                 ch=getc(fp1);
  32.         }
  33.         fclose(fp1);
  34.         fclose(fp2);
  35.         printf("\n files copied");
  36.         }
  37. }

C Program to Find the Size of File using File Handling Function

How to write a C Program to Find the Size of File using File Handling Function in C Programming Language ?

Solution:



/*
 * C Program to Find the Size of File using File Handling Function
 */
#include <stdio.h>
 
void main(int argc, char **argv)
{
    FILE *fp;
    char ch;
    int size = 0;
 
    fp = fopen(argv[1], "r");
    if (fp == NULL)
        printf("\nFile unable to open ");
    else 
        printf("\nFile opened ");
    fseek(fp, 0, 2);    /* file pointer at the end of file */
    size = ftell(fp);   /* take a position of file pointer un size variable */
    printf("The size of given file is : %d\n", size);    
    fclose(fp);
}

C Program Read a char & print next char ( file to file )

How to write a c program to read a char & print next char ( file to file ) in C Programming Language ?


Solution:
/* Read a char & print next char ( file to file ) : A character should be written in the file named "readfile.txt" beforehand and we will take this as input to be manipulated and finally written into the file named "writefile.txt" */