• Advertisement
Sign in to follow this  

Allegro - trying to read text from a file and print on screen

This topic is 4703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all! I need to read text from a normal Windows .txt file and print it on the screen in graphics mode using Allegro, but am stuck. I have been able to load text into a string object: std::ifstream file ("example.txt"); string str; getline(file, str); That works just fine. I can also print a string to the screen, in a separate function: char* str[30]; str[1]=input; textprintf(m_Buffer, font, 0, i*10, WHITE, "Test: %s", str[1] ); This second function has 30 messages stored in an array of pointers to characters, or something. You can see the problem, can't you. I seem to be using two different types of string. Firstly I'm using C++ string objects. Then I'm using something else (the old C style strings? I'm not sure exactlly). The two seem incompatible. Firstly, does anyone know if I can print new C++ strings to the screen in Allegro using textprintf()? If that cannot be done, do I need to convert the string or should I be loading it in from the text file in a different way? *headscratch* *headscratch* Hmm.

Share this post


Link to post
Share on other sites
Advertisement
If you have a string string and want to convert it to a null-terminated c-style string, just do this...


string name("Michael");
char name[8] = name.c_str();




and thats all it takes c_str() returns a null terminated char array string.

[edit] i was beat to it by Roboguy

Share this post


Link to post
Share on other sites
OK, I have tried it like this...

string str="yipee!";
char name[6] = (str.c_str());

But I get a invalid initializer error. Any ideas?

Share this post


Link to post
Share on other sites
What does that do? Create a new c++ style string object, then copy it into a character?

It compiles but it doesn't seem to let me do anything with it. I want to pass it to another function, my function that draws char arrays on the screen. I've tried passing it like this:

m_Test.Input(5, name);
m_Test.Input(5, *name);

Share this post


Link to post
Share on other sites
What do you mean? Does it give a compiler error? Does it crash? Is there a runtime error? Does the m_Test.Input take a const char* or a std::string?

Share this post


Link to post
Share on other sites
m_Test.Input takes a pointer to a string of chars, if my terminology is correct:

void Test::Input(int line, char* input)
{
m_Input[line]=input;
}


If I try to do this:

std::string string = "yipee";
const char* name = string.c_str();
m_Test.Input(1, name);

I get 'invalid converstion from 'const char' to 'char'.


Hmm...

Share this post


Link to post
Share on other sites
Have also tried a more direct approach...

m_Test.Input(1, *string.c_str());

...and get the same.

Am thinking maybe it is better to read the file in using chars instead, it just seems sort of weird to read it in using C++ style string objects then have to convert it to chars before I print it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement