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

This topic is 4644 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 this post ##### Link to post ##### 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 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 That works! Trouble is, although the first integer I'm saving in my BlitzBasic mapper program is the integer 646 (just as a test), when I load in the integer in my C++ file in DevC, it loads in the value 4406688. What could it all mean? Maybe DevC and Blitz handle integers differently? I don't know much about that sort of thing. Here is how I create the file in Blitz: fileout = WriteFile(CurrentDir$()+"testfile.dat")
WriteInt( fileout, 646 )
CloseFile(fileout)

Then here is how I load it in C++:

int data;
std::ifstream file("testfile.dat");
file >> data;
file.close();

Any clues???

##### Share on other sites
My guess is that it is being stored in binary. Go to the file and open it with notepad (Windows will bitch at you about "not being able to open this file", but ignore that[smile]).

If you can see the 646 it's a text file, if it's not readable, it's being stored in binary...

<edit>
If it is stored as binary, try this code:
int data;std::ifstream file( "testfile.dat", std::ios::binary );file.read( (char*) &data, sizeof(int) );
</edit>

##### Share on other sites
It works!!!

You're a genius.

How can I ever repay you?

##### Share on other sites
Quote:
 Original post by joebarnslondonIt works!!!You're a genius.How can I ever repay you?

Well you could rate me up...[smile]

##### Share on other sites
How do I do that?

##### Share on other sites
Click the Rate User option at the bottom of his post.

Cleverly done!