Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to read a negative number from a file

This thread is locked; no one can reply to it. rss feed Print
How to read a negative number from a file
chyuan
Member #7,988
November 2006

How to read a nagetive number, e.g. -1, from a file using file routine provided by allegro?
I read the manual and can not find a statement that can do this

Matthew Leverton
Supreme Loser
January 1999
avatar

chyuan
Member #7,988
November 2006

This is my proc:

#include<allegro.h>
#include <stdlib.h>
int main()
{ BITMAP *buffer=NULL;
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
DATAFILE *mydata=NULL;
mydata=load_datafile("mydata.dat");

buffer=create_bitmap(640,480);
PACKFILE *f;
f=pack_fopen("mydata.dat#stage1","rp");

textprintf_ex(screen, font, 10, 10, makecol(255, 100, 200),-1, "Score: %ld",pack_igetl(f));

readkey();


}END_OF_MAIN();

and the file in the datafile is "-1 -2 3 4"

I tried pack_igetl
but the output is 75708461, looks strange...

CGamesPlay
Member #2,559
July 2002
avatar

If you store it as text, you have to load it into a string buffer (pack_getc repeatedly) and then convert it to a number using something like strtol.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

chyuan
Member #7,988
November 2006

ya... It is just what I am thinking...
It is annoying to process char...

Tobias Dammers
Member #2,604
August 2002
avatar

Then don't store as characters.
Seriously.

File I/O routines have no way of knowing what the data in the file is supposed to represent; from the I/O perspective, a file is just a series of (binary) bytes. It's up to you to use the correct code for loading and writing.

Make sure that you use the same data layout for storing and retrieving. That is, if you store as a string, read as a string; if you store as an int, read as an int.

On a side note: Why do you store scores in a datafile? This is both inconvenient and error-prone. If the computer crashes while writing scores, the datafile may get corrupted. A datafile is meant to hold "read-only" data for a game, but scores are read-write. Better use a dedicated score file (the packfile routines make this pretty easy for you). Plus, to erase the scores, you can just delete the score file instead of messing with the datafile.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

chyuan
Member #7,988
November 2006

Basically, I am making a "Snake Game", (A snake eats an apple and becomes longer.)

I just want to use datafile to store the info. of every stage.
The info. looks like this: (-1 means obstacle that the snake can not pass)

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 0 0 0 0 0 0 0 -1
-1 0 0 0 0 0 0 0 0 -1
-1 0 -1 0 0 0 -1 0 0 -1
-1 0 -1 0 0 0 -1 0 0 -1
-1 0 -1 0 0 0 -1 0 0 -1
-1 0 -1 0 0 0 -1 0 0 -1
-1 0 -1 0 0 0 -1 0 0 -1
-1 0 0 0 0 0 0 0 0 -1
-1 0 0 0 0 0 0 0 0 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

HoHo
Member #4,534
April 2004
avatar

why not use some other number instead of -1? Perhaps you can use 0, 1, 2, ... instead?

Of cource learning to handle binary files would be a bit better in the long run.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

chyuan
Member #7,988
November 2006

I also want to use this way but It mean I have to change a bunch of code which is time consuming and easily make mistake...
I am a lazy man...

HoHo
Member #4,534
April 2004
avatar

You shouldn't hardcode values. You should define those number values as constants somewhere. For example have something like this in your header you include everywhere:

#define CAN_PASS 0
#define CANT_PASS 1

Now when you need to check those values in code you replace 0 with CAN_PASS and 1 (in your current case, -1) with CANT_PASS.

Yes, that means you have to change quite a bit of code but once you are done it should work perfectly and further changes will only mean you have to change the define.

If I misunderstood and you don't have those values hardcoded then please explain why you have to change a lot of code.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

chyuan
Member #7,988
November 2006

not a lot
but still some
anyway, I want to know how to process a negative number in a file...

TeamTerradactyl
Member #7,733
September 2006
avatar

I don't know how to answer this very well, but I had a few ideas:

If you have a signed char, it will (by default) only allow values from -128..127. So if you save a -1 to file, it will save it as an "FF". When you read that back into your program, having the "[signed] char" (signed by default) will automatically assume that a "FF" is your -1...

I'm not seeing why this is not allowing you to correctly save and read a negative number to file...

Are you using an int instead of a char? If so, does your snake game really NEED more than 255 values for your map?

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Are you using an int instead of a char?

No, he's using a string. He is doing this:pack_putc('-'); pack_putc('1'); pack_putc(' ');Yeah, not gonna work ;)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

TeamTerradactyl
Member #7,733
September 2006
avatar

chyuan, if what CGamesPlay is accurate, you can actually kill two birds with one stone:

If you're storing '-' then '1' then ' ' to get "-1 ", you're using up a lot more space than you really need.

(I added padding between the 0's for readability):

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1  0  0  0  0  0  0  0  0 -1
-1  0  0  0  0  0  0  0  0 -1
-1  0 -1  0  0  0 -1  0  0 -1
-1  0 -1  0  0  0 -1  0  0 -1
-1  0 -1  0  0  0 -1  0  0 -1
-1  0 -1  0  0  0 -1  0  0 -1
-1  0 -1  0  0  0 -1  0  0 -1
-1  0  0  0  0  0  0  0  0 -1
-1  0  0  0  0  0  0  0  0 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Looking at this in HEX, you see that this is stored as this (I'm just showing the first 2 lines) in hex:

2D 31 20 2D 31 20 2D 31 20 2D 31 20 2D 31 20 2D 31 20 2D 31 20 2D 31 20 2D 31 20 2D 31 20
2D 31 20    30 20    30 20    30 20    30 20    30 20    30 20    30 20    30 20 2D 31 20
...

If you store this as char values, it would store these values as:

FF FF FF FF FF FF FF FF FF FF
FF 00 00 00 00 00 00 00 00 FF
FF 00 00 00 00 00 00 00 00 FF
FF 00 FF 00 00 00 FF 00 00 FF
FF 00 FF 00 00 00 FF 00 00 FF
FF 00 FF 00 00 00 FF 00 00 FF
FF 00 FF 00 00 00 FF 00 00 FF
FF 00 FF 00 00 00 FF 00 00 FF
FF 00 00 00 00 00 00 00 00 FF
FF 00 00 00 00 00 00 00 00 FF
FF FF FF FF FF FF FF FF FF FF

This takes up a lot less space, AND when you read it in from a file, it automatically converts the "FF" into a "-1", and you don't need an extra "space" to separate the numbers.

It also takes less code to parse the file. If you wanted to keep the same "parsing" code, you could turn it into a "to view/load a map from a text file, please run it through this [some function] to convert from .TXT into a .MAP format... (blah, blah, blah)" conversion function.

It might make your code easier to work with, and then all you need to know how to do is to negate a value whenever you read in a '-' from a file.

CGamesPlay
Member #2,559
July 2002
avatar

No, you can't just put "FF " into the file instead of "-1 ". You need a hex editor.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

TeamTerradactyl
Member #7,733
September 2006
avatar

CGamesPlay said:

No, you can't just put "FF " into the file instead of "-1 ".

Well, that was kind of implied... I mean, "FF" would be two characters (plus a trailing space), and that wouldn't have accomplished anything at all! :P

I just mean that when he read in his game, the values of the chars would all be 'FF' (or 255).

CGamesPlay said:

You need a hex editor chmaas.handshake.de.

I prefer UltraEdit.

Paul whoknows
Member #5,081
September 2004
avatar

You can also try the free and "limited" version of HHD Hex editor really good hexditor!

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Go to: