Allegro - how do I read data from a normal ascii file?

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

Recommended Posts

Hello, I have written a mapper in BlitzBasic which saves data as a normal .dat file. I'm writing my game in C++ using Allegro and I want to be able to access these .dat files. I know you can put files into a sort of databank but I don't wish to do that at this stage. Any ideas on how to load in a normal .dat file and read the integers? Thanks! Joe

Share on other sites
I don't know what you mean by "normal .dat file" but you could just use std::fstream to open and read the file, then parse it however you need.

Hope this helped

Share on other sites
Reading files doesn't change because your using allegro. Also, what do you mean by "normal dat file"? You will need to read the blitzbasic .dat file specs to find out what they contain, wether text or binary, and then write your own loader.

Share on other sites
By normal dat file I just mean that I just opened a new file and saved lots of integers to it, and named it with a .dat extension. I assumed that was a pretty standard thing to do but perhaps not? Anyway it's very simple to do and works very well.

Anyways, so that's the answer: I use std::fstream. I think it should be really straightforward then. Thanks for your help!

Share on other sites
Hmm, having said that...

I've looked around on google and can only find examples of reading text, ie chars, but I want to read integers. Anyone can tell me how to do that? Would be ever so grateful.

Joe

Share on other sites
Thanks!

OK, that gives me the first bit, how to open the file. I can open it like this, I believe:

ofstream file;
file.open("file.dat", ios::in);

But the links don't really explain how to then read the integers.

When I create the file in BlitzBasic it works like this:
fileout = WriteFile(CurrentDir\$()+"file.dat")
WriteInt( fileout, 500 )
WriteInt( fileout, 99 )
CloseFile(fileout)

That would create a file containing the integers 500 and then 99. How do I then read those integers within my C++ programme?

Hope you can help!

Joe

Share on other sites
Quote:
 Original post by joebarnslondonofstream file;file.open("file.dat", ios::in);

You must use fstream, not ofstream (ofstream is for output)

Quote:
 Original post by joebarnslondonBut the links don't really explain how to then read the integers.

It really depends if they are stored as text or binary. If it is text format, it will be quite simple:

int data;
file >> data;

If it is binary, you will have to use the fstream read method.

Share on other sites
OK, I've put this at the start of my program:

#include <fstream>

And if I put this in the main() function it compiles without error:

ifstream file ("001-all.dat");

All well and good. But then, if I move the above line into one of my classes, I get errors:

>'ifstream' undeclared (first use in this function)
>syntax error before '(' token

Is this a structural problem rather than that I have got the ifstream syntax wrong?

I get this error regardless of which file I put the #include in (ie at the start of my main.cpp or in the .cpp or .h of the World class.

Share on other sites
Try putting
std::ifstream file("001-all.dat");
If that does not work, then the problem must be that you need to include fstream in both files...

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 31
• 16
• 11
• 10
• 12
• Forum Statistics

• Total Topics
634117
• Total Posts
3015601
×