Thread: sscanf()-getting integer from a string

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    sscanf()-getting integer from a string

    I want to extract the integer value form my file name, and there's no space between my file name
    like
    part1.dat or part12.dat
    Code:
      char src[20] = "part12.dat";
      char dest[10];
      int *i;
    
      sscanf(src, "%[1-9]",  &i);
      printf("%d", *i);
    could u plz correct my formatted text

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    74
    why a int* ?

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You could use strpbrk() to find digits and use atoi/strtol to convert to int.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Quote Originally Posted by noobiept View Post
    why a int* ?
    because the last argument of the sscanf is a pointer, anyway, the problem is not coming from there, its
    from the formatted string, if i correct it , it would work fine

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Quote Originally Posted by Bayint Naung View Post
    You could use strpbrk() to find digits and use atoi/strtol to convert to int.
    Negative;
    strprbk...() returns a pointer to the first occurence of a character or whatever, but here
    i have no idea what the integer would be

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe:
    Code:
    const char * str = "part346.dat";
    int value;
    
    sscanf(str,"%*[^0-9]%d",&value);
    printf("%d",value);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    74
    Quote Originally Posted by saman_glorious View Post
    because the last argument of the sscanf is a pointer, anyway, the problem is not coming from there, its
    from the formatted string, if i correct it , it would work fine
    Nope, when you do &i, you were getting a pointer to pointer to int.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Quote Originally Posted by hk_mp5kpdw View Post
    Maybe:
    Code:
    const char * str = "part346.dat";
    int value;
    
    sscanf(str,"%*[^0-9]%d",&value);
    printf("%d",value);
    thank u so much for ur help,
    though, i guess i asked a stupid question
    anyway thnks

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yet another cross-poster!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM