• Click below to download Unit 4 programs in pdf file: -




Unit 4


/* 1. Write a program to display contents of file on the screen. The

program should ask for file name. Display the contents in capital

case */


#include <stdio.h>

#include <stdlib.h>


int main()

{

    FILE *fptr;


    char filename[100], c;


    printf("Enter the filename to open : \n");

    scanf("%s", filename);


    fptr = fopen(filename, "r");


    if (fptr == NULL)

    {

        printf("Cannot open file \n");

        exit(0);

    }


    c = fgetc(fptr);

    while (c != EOF)

    {

        printf ("%c", c);

        c = fgetc(fptr);

    }


    fclose(fptr);

    return 0;

}



/* 2. Write a program to find size of the file. */


#include <stdio.h>


long int findSize(char file_name[])

{

    FILE* fp = fopen(file_name, "r");


    if (fp == NULL) {

        printf("File Not Found!\n");

        return -1;

    }


    fseek(fp, 0L, SEEK_END);

    long int res = ftell(fp);

    fclose(fp);

    return res;

}


int main()

{

    char file_name[] = { "1.c" }; // Enter your file name here

    long int res = findSize(file_name);

    if (res != -1)

        printf("Size of the file is %ld bytes \n", res);

    return 0;

}



/* 3. Write a program to combine contents of two files in a third file.

Add line number at the beginning of each line. */


#include <stdio.h>

#include <stdlib.h>


int main()

{

   FILE *fp1 = fopen("file1.txt", "r"); // Enter your file name here

   FILE *fp2 = fopen("file2.txt", "r");


   FILE *fp3 = fopen("file3.txt", "w");

   char c;


   if (fp1 == NULL || fp2 == NULL || fp3 == NULL)

   {

         puts("Could not open files");

         exit(0);

   }


   while ((c = fgetc(fp1)) != EOF)

      fputc(c, fp3);


   while ((c = fgetc(fp2)) != EOF)

      fputc(c, fp3);


   printf("Merged file1.txt and file2.txt into file3.txt");


   fclose(fp1);

   fclose(fp2);

   fclose(fp3);

   return 0;

}



/* 4. Write a program to display number 1 to 100. Redirect the output

of the program to text file. */



/* 5. Write a program to write contents of one file in reverse into

another file. */


#include<stdio.h>

#include<errno.h>


long count_characters(FILE *);


void main()

{

    int i;

    long cnt;

    char ch,ch1;

    FILE *fp1,*fp2;



if(fp1=fopen("myfile.txt","r")) //Enter your file name here

{

      printf("THE FILE HAS BEEN OPENED.\n");


      fp2=fopen("reverse.txt","w");


      cnt=count_characters(fp1);


      fseek(fp1,-1L,2);


      printf("NUmber of Characters To be copied %d\n",ftell(fp1));


  while(ch)

  {

      ch=fgetc(fp1);

      fputc(ch,fp2);

      fseek(fp1,-2L,1);

      cnt--;

  }

      printf("\n--FILE COPIED SUCCESSFULLY IN REVERSE.--\n");

  }

  else

  {

       perror("Error Occured\n");

  }

      fclose(fp1);

      fclose(fp2);


  }


long count_characters(FILE *f)

  {

      fseek(f,-1L,2);

      long last_pos=ftell(f);

      last_pos++;

      return last_pos;

  }



/* 6. Write a program to count number of lines, words and characters

in a file. */



#include <stdio.h>

#include <stdlib.h>


int main()

{

    FILE * file;

    char path[100];


    char ch;

    int characters, words, lines;


    printf("Enter file name : ");

    scanf("%s", path);


    file = fopen(path, "r");


    if (file == NULL)

    {

        printf("\nUnable to open file.\n");


        exit(EXIT_FAILURE);

    }


    characters = words = lines = 0;

    while ((ch = fgetc(file)) != EOF)

    {

        characters++;


        if (ch == '\n' || ch == '\0')

            lines++;


        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')

            words++;

    }


    if (characters > 0)

    {

        words++;

        lines++;

    }


    printf("\n");

    printf("Total characters = %d\n", characters);

    printf("Total words      = %d\n", words);

    printf("Total lines      = %d\n", lines);


    fclose(file);


    return 0;

}



/* 7. Write a program to create a file called dictionary.dat that

contains the information such as Name, Surname, City and Phone

number. Write a program to accept a City from user and list details

of persons having the given city. */



/* 8. Write a program to copy one file to another. While doing so, all

extra spaces in a file should be squeezed to one. For eg. If a file

contains line “I am learning C”, it should be converted to “I am

learning C”. */



#include <stdio.h>


void main(int argc,char *argv[])

{

 FILE *fp1,*fp2;

 char ch,word[20]=" ",temp[4]=" ";

 int a=0,b,count=0;


 if(argc!=3)

 {

 printf("Invalid command syntax");

 }

     fp1=fopen(argv[1],"r");

     fp2=fopen("word.txt","a");

     strcpy(word,argv[2]);


 if(fp1==NULL)

     {

       printf("\nFile not found.");

     }


 if(fp2==NULL)

     {

     printf("\nError in creating output file 'word.txt'");

     }


 while((strcmp("",word)!=0))

     {

     while(!feof(fp1))

     {

          for(b=0;b<3;b++)

          {

           ch=fgetc(fp1);


           if(ch==' '||ch=='\n')

            break;

           else

            temp[a++]=ch;

     }

    temp[3]='\0';

    if(word[0]==temp[0]&&word[1]==temp[1]&&word[2]==temp[2])

    count++;

      strcpy(temp," ");

      a=0;

     }


     fprintf(fp2,"%s\t- %d\n",word,count);


     printf("\nFrequncy of the word '%s' is saved in file 'word.txt'",word);

     printf("\n\nEnter another word : ");


     gets(word);

     count=0;

     rewind(fp1);

 }

}



/* 9. Write a program that counts the frequency of a word from a text

file. The program should accept file name as commandline

argument. Program should continue to ask word and display its

frequency in a file till the Enter key is pressed without entering any

word. */



/* 10 Write a Program to insert the following contents in a file named

“File1”.

Customer No. Account Type Balance

101 Savings 2000

102 Current 5000

103 Savings 3000

104 Current 10000

Append the contents of “File1” in another file “File2”. Also display

the contents of File2 on screen.

*/


#include <stdio.h>

#include<conio.h>

void main()

{

 FILE *fp1,*fp2;

 struct file{

  int cno,bal;

  char actype[8];

 }cdetails[4]={

 {101,2000,"Savings"},

 {102,5000,"Current"},

 {103,3000,"Savings"},

 {104,10000,"Current"}

 };

 char ch;

 int a;


 fp1=fopen("file1.txt","w");

 fp2=fopen("file2.txt","a+");


 if((fp1||fp2)==NULL)

 {

 printf("\nError in handling file(s).");


 }

 fprintf(fp1,"Customer No.\tAccount Type\tBalance\n");

 fprintf(fp2,"Customer No.\tAccount Type\tBalance\n");


 for(a=0;a<4;a++)

 {

 fprintf(fp1,"%d\t\t",cdetails[a].cno);

 fprintf(fp1,"%s\t\t",cdetails[a].actype);

 fprintf(fp1,"%d\n",cdetails[a].bal);

 fprintf(fp2,"%d\t\t",cdetails[a].cno);

 fprintf(fp2,"%s\t\t",cdetails[a].actype);

 fprintf(fp2,"%d\n",cdetails[a].bal);

 }

 printf("\nData copied to file1.txt successfully.");

 printf("\nContents of file1.txt appended to file2.txt successfully.");

 printf("\nContents of file2.txt :\n\n");

 rewind(fp2);


 while(!feof(fp2))

 {

 ch=fgetc(fp2);

 printf("%c",ch);

 }

}