Thread: possible use of atoi()

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    possible use of atoi()

    Hey all! Spend 3 months since October reading my 700 page book for the second time, now I am on the second to last question before I finish it,...would like to finish the last exercise which includes writing a complete program. Would like to inventory my groceries I have in the freezer, refrigerator, and also in the dry stock shelf.

    Just a quick question on this code:


    Code:
    // commandargs.c--Accessing command-line arguments.
    
    #include <stdio.h>
    #include <errno.h>
    
    int file_copy( char *oldname, char *newname );
    
    int main(int argc, char *argv[])
    {
        int a, b;
        int count;
        char source[80], destination[80];
    
        /* Get the source and destination names. */
    
        if (argc < 2 || argc > 3)
        {
            printf("Enter 2 arguments only\n");
        }
    
        a = atoi(argv[1]);
        b = atoi(argv[2]);
    
        if( file_copy( a, b ) == 0 )
            puts("Copy operation successful");
        else
            fprintf(stderr, "Error during copy operation");
        return 0;
    }
    
    int file_copy( char *oldname, char *newname )
    {
        FILE *fold, *fnew;
        int c;
        
        /* Open the source file for reading in binary mode. */
    
        if ( ( fold = fopen( oldname, "rb" ) ) == NULL )
            return -1;
    
        /* Open the destination file for writing in binary mode. */
    
        if ( ( fnew = fopen( newname, "wb" ) ) == NULL )
        {
            fclose ( fold );
            return -1;
        }
    
        /* Read one byte at a time from the source; if end of file */
        /* has not been reached, write the byte to the */
        /* destination. */
    
        while (1)
        {
            c = fgetc( fold );
    
            if (!feof( fold ) )
                fputc( c, fnew );
            else
                break;
        }
    
        fclose ( fnew );
        fclose ( fold );
    
        return 0;
    }
    Here..

    Code:
        a = atoi(argv[1]);
        b = atoi(argv[2]);
    Compiler is complaing about using atoi() in C99, not even sure if this is the right function, and was also wondering if strtol() would be.

    Also...

    Code:
    [jamie@bindfix Lesson_22]$ cc -o ex_5_2 ex_5_2.c
    ex_5_2.c:21:6: warning: implicit declaration of function 'atoi' is invalid in C99 [-Wimplicit-function-declaration]
            a = atoi(argv[1]);
                ^
    ex_5_2.c:24:17: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'char *' [-Wint-conversion]
            if( file_copy( a, b ) == 0 )
                           ^
    ex_5_2.c:6:22: note: passing argument to parameter 'oldname' here
    int file_copy( char *oldname, char *newname );
                         ^
    ex_5_2.c:24:20: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'char *' [-Wint-conversion]
            if( file_copy( a, b ) == 0 )
                              ^
    ex_5_2.c:6:37: note: passing argument to parameter 'newname' here
    int file_copy( char *oldname, char *newname );
                                        ^
    3 warnings generated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When you see "implicit declaration of function", check that you have included the necessary headers or forward declared the function. In this case, you need to #include <stdlib.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Atoi and such
    By Khabz in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2013, 02:56 PM
  2. use of atoi.
    By mgracecar in forum C Programming
    Replies: 3
    Last Post: 03-06-2012, 09:02 PM
  3. C++.NET Can I use atoi ??
    By Swaine777 in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2004, 06:54 PM
  4. atoi
    By Max in forum C Programming
    Replies: 1
    Last Post: 11-05-2002, 10:28 AM

Tags for this Thread