Thread: use of atoi.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    use of atoi.

    Messing around with atoi from grabbing a number off of a file. First is there a way to just grab it off the file in the form of an int instead of a string? If so that's easier than my method. If not...with my current program I'm getting an error stating...

    "passing arg 1 of 'atoi' from incompatible pointer type"
    How can I fix this?

    Code:
    int main(void)
    {
    FILE *fp;
    char buffer[50];
    char *number;
    int i;
    
    fp = fopen("file04.txt", "r");
       
       while (fgets(buffer, sizeof(buffer), fp)!=NULL)
       {
            sscanf(buffer,"%s", &number);
            i = atoi(&number);
       }           
    fclose(fp);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fscanf - Check the man page. fscanf

    As to your error, get rid of the &. & means 'address of' in that case, and you don't want the address of that pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    get rid of it in the sscanf? or the atoi?
    because when I get rid of the & in the atoi, the program just crashes with no error.

    edit...nvm found what I did wrong. Had a char type as an int. Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your only pointer is 'number'. In both cases you are putting the address of the pointer into functions that don't want the address of a pointer. They want the value the pointer holds. Except that your pointer doesn't actually hold a valid address, because you've never made it point anywhere.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++.NET Can I use atoi ??
    By Swaine777 in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2004, 06:54 PM
  2. atoi?
    By mayfda in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2002, 09:45 AM
  3. atoi
    By Max in forum C Programming
    Replies: 1
    Last Post: 11-05-2002, 10:28 AM
  4. atoi()
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 07-13-2002, 02:09 AM
  5. atoi
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 02:39 AM