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

Started by
11 comments, last by darenking 18 years, 10 months ago
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.
Advertisement
std::string::c_str() returns a constant character pointer.
I don't really understand. Does it point to the C++ string object? And therefore can be used in the Allegro textprintf()?
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
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?

Try:
std::string string = "yipee";const char* name = string.c_str();
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);

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?
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...
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.

This topic is closed to new replies.

Advertisement