Monday, May 09, 2011

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

Hello Surya,

If you do not mention an output file name using the option '-o' then
the default output file 'a.out' is created. But you can mention the
output file name using '-o' immediately followed by the name, which in
this case, was 'filecopy'. So you can then run the program using the
command:

./filecopy

Now, you are expecting 2 input files to be given to the main. So those
two file names are to be provided. If the files are not in the same
directory as the program, you can always give the full path, like,

./filecopy file1.txt /home/minhaz/Desktop/file2.txt

Hope it helps.

Thanks.

--
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.

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

Mr. Alam,

Thanks a lot for helping me!

gcc -o filecopy your_cpp_file_name

./filecopy file1.txt file2.txt


Actually I even didn't get any errors in the code but I didn't know how to execute it.
As I am new to Linux environment, I did not know how to deal with it.
This is what I have done at terminal.

gcc filename_source.c
<then I again used >
./a.out < which I usually do for getting output.

But Mr. Alam can you explain me why you have written the code that you mentioned instead of using the one I mentioned above. I have seen that there is a file created 'filecopy' in the default location just like 'a.out'.
And also please tell me how do I deal with files which are not present in default locations.

Thanks!

--
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.

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.

Sunday, May 08, 2011

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

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.