Monday, May 09, 2011

Re: [Cpp-Programming] file copy program with arguments taken in main function

Hello Surya,

I just ran your code in a windows machine and its working fine here. Will you plz mention what error message you get when you running your code? And also plz note that, you have to keep the files 'file1.txt' and 'file2.txt' in the same directory as the executable program 'filecopy'. I am assuming that you compiled your cpp file using the following command:

gcc -o filecopy your_cpp_file_name

and then run the program using the command:

./filecopy file1.txt file2.txt

Thanks.

On Mon, May 9, 2011 at 12:02 AM, surya <kasturisurya@gmail.com> wrote:
This what my code is :
===========================
# include <stdio.h>
# include <stdlib.h>

int main ( int argc, char *argv[] )
{
   FILE *psCopy, *psPaste ;
   char ch ;

  /* if ( argc != 3 )
     {
       printf("Insufficient number of arguments\n");
       exit (101);
     }  */

   psCopy = fopen ( argv [1], "r" );
   if ( !psCopy )
     {
       printf ("File couldn't open. Sorry!\n");
       exit (102);
     }

   psPaste = fopen ( argv [2], "w" );
   if ( !psPaste )
   {
      printf ("File couldn't open. \n");
      exit (201);
   }

   while ( (fscanf (psCopy,"%c",&ch)) == 1 )
     fprintf (psPaste,"%c",ch) ;

  fclose (psCopy) ;
  fclose (psPaste) ;

return ;
}
=========================
I use GNU gcc Compiler in Linux system.

Now The code basically should take 2 files as input and copy from one
file to another. So, I opened terminal at entered < filecopy file1.txt
file2.txt > but its showing some errors.
How should I do run this in terminal so as to successfully run this
code.
< source code file, file1.txt and file2.txt are stored in the default
location where generally C stores a.out >

--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.




--
Mohammad Minhazul Alam
Lecturer, Dept. of CSE & IT, UITS


--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.

0 Comments:

Post a Comment

<< Home