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

    Reading hexadecimal values for a file

    Hi! I'm trying to read some values stored in a file.The file looks like this:

    GENIUS 0 0x0080000000000000 black_pawn1
    GENIUS 1 0x0040000000000000 black_pawn2

    .................................................................................................................

    and it stores the startup positions for the chess pieces.
    I tryed something like this but it doesn't work:
    <h3>fscanf(fp,"%d%xll",&id,&move);</h3> where ll is for unsigned __int64 or at least this is what i want. it to be(in case i'm wrong)
    //**********************************************************
    FILE* fp;
    char buffer[100];
    unsigned __int64 move;
    unsigned int id;
    CList* node;


    fp=fopen("startup.txt","rt");
    if(!fp)return false;


    while(!feof(fp))
    {

    fscanf(fp,"%s",buffer);
    fscanf(fp,"%d%xll",&id,&move);
    node=new CList(move,id);


    if(!stricmp(buffer,"GENIUS"))
    {
    geniusList.Insert(node);
    }else{

    if(!stricmp(buffer,"MINIME"))

    {


    minimeList.Insert(node);

    }






    }









    fgets(buffer,100,fp);






    }








    return true;

    }





    ThankX!

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    Give up fscanf and FILE and use fstream from STL.

    Second, try to make your posts more appealing by using tags, such as CODE, QUOTE, etc.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Reading hexadecimal values for a file

    Also give up on CList and use STL collections, std::list if appropriate.

  4. #4
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58

    Re: Reading hexadecimal values for a file

    Quote Originally Posted by Daos
    Hi! I'm trying to read some values stored in a file.The file looks like this:

    GENIUS 0 0x0080000000000000 black_pawn1
    GENIUS 1 0x0040000000000000 black_pawn2

    .................................................................................................................

    and it stores the startup positions for the chess pieces.
    I tryed something like this but it doesn't work:
    fscanf(fp,"%d%xll",&id,&move); where ll is for unsigned __int64 or at least this is what i want. it to be(in case i'm wrong)
    You never said if the compiler was MS VC++ (I'll assume yes given the __int64), so here is the quick answer:
    Code:
    fscanf(fp, "%d 0x%16I64X, &id, &move);
    where id is an int and move is __int64.
    Code:
    Here is the breakdown:
    
      " 0x" to match the space and 0x in your example
      "%16I64X" 16 - the size for a 64 bit integer
               I64 - To tell VC++ compiler the variable is __int64
                 X - the string is hexadecimal values

  5. #5
    Join Date
    Oct 2003
    Posts
    83

    Re: Reading hexadecimal values for a file

    What if one would like to have the value of an _int64 displayed in decimal form.
    What the string formatting should be :
    Would it be :
    myString.Format("%16I64d",mylonglong);
    Thanks for any help.

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Reading hexadecimal values for a file

    You could use fstream for reading in the data but personally I dislike reading this way much of the time - I prefer to read a line at a time then tokenize it. boost::tokenizer is good for this but if you don't have that, it is simple enough to write your own in a couple of lines with std::string's "find" or "find_first_of" command.

    The "0x" is used only for code and isn't part of the input/output of a hex number. You'd have to cut that off before converting (which is why tokenizing can be good in this instance).

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