Sunday, November 20, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 12 - C Program Write a program which takes a file as command line argument and copy it to another file. At the end of the second file write the total i) no. of chars ii) no. of words and iii)no. of lines

Bharathiar University

Programming in C Lab
C Program 12
Write a program which takes a file as command line argument and copy it to another file. At the end of the second file write the total i) no. of chars ii) no. of words and iii)no. of lines


Source Code
//Program with File Location

#include <stdio.h>

int main(int argc,char * argv[])
{
FILE *ptr1, *ptr2;
char ch;
int letters=0,words=0,lines=0;
    // Opening file1 in reading mode
ptr1 = fopen(argv[1], "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
    // Opening file2 in write mode
ptr2 = fopen("C:\\Users\\Admin\\Desktop\\sample.txt", "w");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// copying contents from one to another file
while((ch= fgetc(ptr1)) != EOF){
//ch = fgetc(ptr1);
fputc(ch,ptr2);
//count characters
   letters++;
        /* Check new line */
        if (ch == '\n' || ch == '\0')
            lines++;
        /* Check words */
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
            words++;
  }
  if(letters>0)
    {
        words++;
        lines++;
    }
    fprintf(ptr2,"\nTotal Number of Characters:");
    fprintf(ptr2,"%d",letters);
    fprintf(ptr2,"\nTotal Number of Words:");
    fprintf(ptr2,"%d",words);
    fprintf(ptr2,"\nTotal Number of Lines:");
    fprintf(ptr2,"%d",lines);
    printf("Contents written in sample.txt file successfully");
// Closing the file
    fclose(ptr1);
fclose(ptr2);
    getch();
return 0;
}

To set Command line arguments

Run menu --> Arguments

Command line Argument with file location


Source Code

//Program without File Location

#include <stdio.h>

int main(int argc,char * argv[])
{
FILE *ptr1, *ptr2;
char ch;
int letters=0,words=0,lines=0;
    // Opening file1 in reading mode
ptr1 = fopen(argv[1], "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
     // Opening file2 in write mode
ptr2 = fopen("sample.txt", "w");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// copying contents from one to another file
while((ch= fgetc(ptr1)) != EOF){
//ch = fgetc(ptr1);
fputc(ch,ptr2);
//count characters
   letters++;
        /* Check new line */
        if (ch == '\n' || ch == '\0')
            lines++;
        /* Check words */
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
            words++;
  }
  if(letters>0)
    {
        words++;
        lines++;
    }
    fprintf(ptr2,"\nTotal Number of Characters:");
    fprintf(ptr2,"%d",letters);
    fprintf(ptr2,"\nTotal Number of Words:");
    fprintf(ptr2,"%d",words);
    fprintf(ptr2,"\nTotal Number of Lines:");
    fprintf(ptr2,"%d",lines);
    printf("Contents written in sample.txt file successfully");
// Closing the file
    fclose(ptr1);
fclose(ptr2);
    getch();
return 0;
}


Output:
Contents written in sample.txt file successfully


To set Command line arguments

Run menu --> Arguments

Command line Argument without file location

Input File



Output File

No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...