CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    38

    fscanf issues (again)

    hello i am a bit confused here
    all i want to do is open a file and read the first line from it which happens to be a hexidecimal number say C8

    it sort of works but the value that is printed is not 200
    (which is the decimal equiv. to C8)


    int GetValue(FILE* fptr){

    int val;
    char buff[100];

    start = 0;
    fgets(buff,95,fptr);
    sscanf(buff, "%d", &val);
    printf("hex value: %0xd\n", val);
    printf*("dec value: %d\n",val);
    }

    it outputs 10d or 0d
    or something else am i not initializing something somewhere?

    can someone point to what i am doing wrong here?

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Replace
    sscanf(buff, "%d", &val);
    with
    sscanf(buff, "%x", &val);

    Good luck,
    Jonas

  3. #3
    Join Date
    Aug 2002
    Posts
    78
    %d in scanf tells it to look for a decimal number in the input, not a hexadecimal number.

    If trying to read "C8", the scanf would first see the "C", and choke because that isn't a digit, which it is expecting. What it puts into the variable "val" is a good guess. Might be 0, might be unchanged (i.e. garbage in this example.) Might be the number of jelly beans in a jar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured