0

I came across a question to copy contents from one file to another using command line arguments in C. It is as follows:

Q: Write a C program to copy contents of one file to another using command line arguments in C. The following output should be obtained:

1) >>> ./a.out
     Display: "Insufficient arguments"

2) >>> ./a.out a.txt b.txt
     Display: File not created

3) >>> ./a.out a.txt b.txt
     Display: File is empty

4) >>> ./a.out a.txt b.txt     
     Display: File successfully copied
5) >>> b.txt
     Display: Hello World.

The code that I have written for the above question is:

#include<stdio.h>
#include<stdlib.h>
main(int argc,char *argv[])
{
 FILE *fp1,*fp2;
 char ch;
 if(argc!=3)
 {
  printf("\n insufficient arguments.\n");
  exit(0);
 }
 fp1=fopen(argv[1],"r");
 fp2=fopen(argv[2],"w");
 if(fp1==NULL || fp2==NULL)
 {
   printf("\n File not created.\n");
   exit(0); 
 }
 if (NULL != fp1) 
 {
    fseek (fp1, 0, SEEK_END);
    int size = ftell(fp1);
    if (0 == size)
    {
     printf("File is empty.\n");
     exit(0);
    }
 }
while(!feof(fp1))
{
 ch=fgetc(fp1);
 fputc(ch,fp2);
}
printf("\n File successfully copied ");
fclose(fp1);
fclose(fp2);
}

When I run the above code I get the output as:

   sh-4.2$ gcc main.c
   sh-4.2$ ./a.out

   Insufficient arguments.

   sh-4.2$ gcc main.c
   sh-4.2$ ./a.out a.txt b.txt

   File not created.

   sh-4.2$ gcc main.c
   sh-4.2$ ./a.out a.txt b.txt    // I created one empty file a.txt 

   File is empty. 

   sh-4.2$ gcc main.c                                         
   sh-4.2$ ./a.out a.txt b.txt   // Saved "Hello World" to a.txt and created empty file b.txt

   File successfully copied

   sh-4.2$ gcc main.c
   sh-4.2$ b.txt                // Why can't I carry out this operation as given in question?
   sh: ./b.txt: Permission denied

   sh-4.2$ gcc main.c
   sh-4.2$ cat b.txt
   � 
   sh-4.2$ cat a.txt
   Hello World

I got this symbol: � when I tried to display contents of b.txt. But when I displayed contents of a.txt I got Hello World. I couldn't display contents of b.txt by simply typing its name as given in the question. It came only after I included 'cat' command before the file name.

I would like to solve the problem in the most simplest way possible. Have I missed any logic or any line in the code?

Animeartist
  • 1,047
  • 1
  • 10
  • 21
  • "`sh-4.2$ b.txt` Why can't I carry out this operation". You must have misread the requirements or the requirements have an error. It can't be telling you to run a text file. – kaylum Apr 30 '17 at 09:53
  • `fseek (fp1, 0, SEEK_END)`. That leaves the file pointer at the end of the file. You need to `fseek` or `rewind` back to the beginning before reading it again. – kaylum Apr 30 '17 at 09:55
  • When I used rewind(fp1); and displayed contents of b.txt, I am getting it like : Hello World �. The symbol is coming again. How can I remove this symbol? – Animeartist Apr 30 '17 at 11:25
  • Try using hexdump on b.txt and compare it to the hexdump of a.txt, post the results. – Felix Guo Apr 30 '17 at 17:06
  • See [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). Also, `fgetc` and `fputc` return and take `int` respectively and not `char`. These may or may not fix your problem but need to be fixed first and then re-test. – kaylum Apr 30 '17 at 20:53

0 Answers0