Thread: Help: Find a integer digit in a string

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Help: Find a integer digit in a string

    Hi.
    I need to know if the very first character each line of a .TXT file is a int (from 0 to 9)?
    In the following code i think the program give me the first character, but how do I know if it is a integer?

    Code:
     
    sscanf(file, "%d, &x);
      if (file[0] == "%*[^0-9]%d")
         {
    	printf("%d\n", x);
         }
    Thanks in advance.

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    You could use the isalpha() C function.

    i.e.
    Code:
    if( !isalpha(...) )

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    I think isalpha() is for C++. I'm working on C.
    But thanks anyway.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Vivasso View Post
    I think isalpha() is for C++. I'm working on C.
    But thanks anyway.
    Nope it's C. #include <ctype.h>

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Yes you are right is for C. But doesn't help me because it checks if it is a alphanumeric character, or not.... A character like "#" it's not alphanumeric, and it's not what I want. I need to know only if it it belongs to [0,9].

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try the isdigit() function then... same include file.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Do I need to use atoi function?
    [code]
    if isdigit (file[0] =0)
    than....
    [\code]
    it's enough?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    I think it's working....I will be back to confirm.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    OK Thank's it works.
    Case close!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

Tags for this Thread