Jump to content
  • Advertisement
Sign in to follow this  
darenking

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

This topic is 4740 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, 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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:

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

Share this post


Link to post
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...

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!