Like Tree1Likes
  • 1 Post By Bayint Naung

file copy program with arguments taken in main function

This is a discussion on file copy program with arguments taken in main function within the C Programming forums, part of the General Programming Boards category; I use Linux system with GNU gcc compiler. I have written the following code for copying string of one file ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    Exclamation file copy program with arguments taken in main function

    I use Linux system with GNU gcc compiler.

    I have written the following code for copying string of one file to another.
    -this code mainly take two file names as input and copies from one to another.
    > the program is compiled and has no errors but the problem is that when I first give input in the terminal like
    PHP Code:
    filecopy file1.txt file2.txt 
    Its saying no command exists....
    the same I used in windows dos and it worked.

    what kind of command should I use while giving input ?
    Code:
    # 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 ;
    }
    Last edited by Salem; 05-08-2011 at 07:53 AM. Reason: Remove unnecessary tags

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Make sure you are in the directory where your executable is.
    Then

    Code:
    ./program  file1.txt file2.txt
    Why not use fread/fwrite?!
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Yes, I saved all my .c files and the .txt files in the default location.
    1. I used the code that you mentioned and its saying
    bash: ./program: No such file or directory
    I then used
    ./
    and then
    ./home/suryak (which is my default directory
    but I am not getting it...

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    when I use
    Code:
    ./a.out
    which I use to get output, its returning to what I have written
    File couldn't open. Sorry!
    in the source code.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use the ls command or a filebrowser so you can see what does and does not exist in your current working directory. Gcc by default always creates a file called "a.out". If you want to call it something else, use "-o" to specify:

    Code:
    gcc -Wall mysource.c -o programname
    Always use "-Wall" with gcc too. If the program cannot open the file, you should tell the user a bit more about why:

    Code:
    #include <errno.h>
    
    [.....]
    
        if ( !psCopy )
          {
            printf ("File couldn't open: (%d) %s\n", errno, strerror(errno);
            exit (102);
          }
    This may help to explain the issue.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So, it looks like your first file is not valid, you can confirm this by doing:

    Code:
    echo $?
    after you have tried the program since you return unique numbers on each test.

    Could be anything from that the file doesn't exist in the current directory to a spelling error.
    Last edited by Subsonics; 05-08-2011 at 08:36 AM. Reason: the

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I'm not able to fix with them and I feel so confused.
    Please tell me clearly where I'm doing wrong and what should I do.
    Assuming all my source code file and the files that I am using for copying are in the default location where generally a.out is present.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you have a.out and two files called file1.txt and file2.txt in your current directory, (confirm by ls as suggested by mk27). Then this should work:

    Code:
    ./a.out file1.txt file2.txt
    This will copy the content of file1.txt to file2.txt you can confirm this by doing:

    Code:
    cat file2.txt

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by suryak View Post
    I'm not able to fix with them and I feel so confused.
    Please tell me clearly where I'm doing wrong and what should I do.
    Okay...

    Assuming all my source code file and the files that I am using for copying are in the default location where generally a.out is present.
    If you are assuming that, that is what you are doing wrong. Assuming things are how you think they should be will get you nowhere fast You need to make certain of what you are doing.

    If you mean that I should assume that, then I will believe you. Did you try using strerror() like I suggested before? What error did you get? BTW (sorry) I forgot you need string.h and errno.h to use strerror():

    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    
    int main(void) {
    	FILE *dud = fopen("non-existent file", "r");
    
    	if (!dud) printf("Errno = %d\n%s\n", errno, strerror(errno));
    
    	return 0;
    }
    Here's what that does for me:
    Code:
    [root~/C] gcc -Wall test.c -o example
    [root~/C] ./example
    Errno = 2
    No such file or directory
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by suryak View Post
    when I use
    Code:
    ./a.out
    which I use to get output, its returning to what I have written in the source code.
    Don't forget that linux is case sensitive... File1.txt is not the same thing as file1.txt...
    Are you typing the file names in their approriate cases?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-04-2010, 03:19 AM
  2. having some trouble calling a function in the main program
    By CMakesMeSad :( in forum C Programming
    Replies: 19
    Last Post: 06-26-2009, 09:33 PM
  3. Replies: 2
    Last Post: 03-30-2009, 02:06 AM
  4. Too many arguments to function int main?
    By plain in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2006, 06:01 PM
  5. main function arguments
    By myjay in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2003, 08:33 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21