Load a string from a datafile
James Stanley

Would it be possible to load a string from a datafile?
I run grabber, and open a text file as binary, save and load the datafile like normal, and then try to load the string into the program with the following code:

char *help_text;
help_text = (char*)dat_menu[MNU_HELP].dat;

But there's nothing in help_text. Am I doing something wrong?

It turns out that there was something in help_text, but there was nothing in dialog_help[HELP_TXT].dp, because I'd initialised the dialog BEFORE loading the datafile information into help_text. It's been fixed now.

Trent Gamblin

You need to register a loader with Allegro. Use something like this:

void* ReadStringFromPackfile(PACKFILE* f, long size)
  char* s = new char[size+1];
  int i;
  for (i = 0; i < size; i++)
    s<i> = pack_getc(f);
  s<i> = 0;
  return (void*)s;

and a function to destroy the string:

void DestroyString(void* data)
  delete (char*)data;

Then call register_datafile_object with a line something like this:

register_datafile_object(DAT_ID('T', 'E', 'X', 'T'), ReadStringFromPackfile,

Now give the text objects the type "TEXT" and load your datafile and dat_menu[MNU_HELP].dat will be a pointer to the character string.

Andrei Ellman
Trent Gamblin said:

You need to register a loader with Allegro. Use something like this:

For simple text data, this is overkill. The method that James uses is adequate, and is the method I use to retrieve text-data from datafiles.


Dustin Dettmer

Beware of newline problems...

Trent Gamblin

Oh, I didn't know Allegro would read it in as raw data without registering a loader. I guess I should have read the manual.

James Stanley

Newline isn't a problem for me (and there's plenty of them :))

I didn't read the manual, I just worked it out for myself:
datafile stuff is (as far as C++ is concerned) of type void.
Void is essentially binary data, so is char, but it's handled differently
1 + 1 = convert to char and see how it goes!
I think that's right, I don't really care, but my method works

Thread #585658. Printed from Allegro.cc