Sign in to follow this  
darenking

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

Recommended Posts

darenking    122
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
neodingo    103
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
darenking    122
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
Roboguy    794
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
darenking    122
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
darenking    122
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
darenking    122
OK, anyone who's still listening...

I've just gone down the other route of reading in old C style chars.

char buffer[256];
ifstream example("filename.txt");
if(example.fail() )
{
m_Test.Input(1, "Error opening file");
}
else
{
m_Test.Input(1, "File open");
example.getline(buffer, 250);
}
example.close();

(You will note that I am using my Test object, which stores error messages etc.)

OK, that should put the first line of the text file into the char string called 'buffer', shouldn't it. But it's still not quite right as Allegro (so far as I can tell) will only output text from a pointer to an array of characters. I've tried to pass it to my method like this:

m_Test.Input(0, buffer);

But it just seems to send a blank line or something, maybe just NULL.

I've tried passing a pointer to it:

m_Test.Input(5, *buffer);

But get told off for trying to convert a char to a char!

Help!




Share this post


Link to post
Share on other sites
Roboguy    794
Quote:
Original post by joebarnslondon
I've tried passing a pointer to it:

m_Test.Input(5, *buffer);
That dereferences the pointer.

Also, try changing

void Test::Input(int line, char* input)

to

void Test::Input(int line, const char* input)

and

m_Input[line]=input;

to

strcpy(m_Input[line], input);

(Note: You'll need to include <cstring> to use strcpy).

Share this post


Link to post
Share on other sites
darenking    122
Thanks Roboguy!

I'm finding this too complex; I think I'm going to try storing the whole thing as a C++ string object right up to the point where I have to display it on the screen. I'm sure this hasn't been a wasted conversation though. I'm a bit lost but I expect it will be useful to someone else.

I'm going to post another post about displaying string objects in Allegro (if I can't find it on google).

Thanks all!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this