Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Load a string from a datafile

This thread is locked; no one can reply to it. rss feed Print
Load a string from a datafile
James Stanley
Member #7,275
May 2006
avatar

Hi,
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?

EDIT:
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
Member #261
April 2000
avatar

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,
  DestroyString);

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
Member #3,434
April 2003

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.

AE.

--
Don't let the illegitimates turn you into carbon.

Dustin Dettmer
Member #3,935
October 2003
avatar

Beware of newline problems...

Trent Gamblin
Member #261
April 2000
avatar

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
Member #7,275
May 2006
avatar

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

Trent:
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

Go to: