|
-
August 24th, 2004, 08:19 AM
#1
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!
-
August 24th, 2004, 09:04 AM
#2
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.
-
August 24th, 2004, 11:36 AM
#3
Re: Reading hexadecimal values for a file
Also give up on CList and use STL collections, std::list if appropriate.
-
August 24th, 2004, 04:25 PM
#4
Re: Reading hexadecimal values for a file
 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
-
April 14th, 2005, 11:47 AM
#5
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.
-
April 14th, 2005, 06:29 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|