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

Started by
15 comments, last by darenking 18 years, 10 months ago
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
Advertisement
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
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.
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!
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

Try these links:

c++ style:
http://www.cplusplus.com/doc/tutorial/tut6-1.html

c style:
http://www.cprogramming.com/tutorial/cfileio.html
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





Quote:Original post by joebarnslondon
ofstream file;
file.open("file.dat", ios::in);


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

Quote:Original post by joebarnslondon
But 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.
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:

>In member function 'World::LoadMap()':
>'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.
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...

This topic is closed to new replies.

Advertisement