Sign in to follow this  
Sir Sapo

fstream problem

Recommended Posts

I have been trying to solve this problem for about an hour now, and I have run out of steam. In the engine I am making, levels are saved as .txt files, with variables saved in a certain order. In all my other games, this worked great, and I could load up levels by creating an ifstream to the the text file, and reading in my variables. However, with my new project, I have a problem. I created a text filewith the variables in it, and I created a ifstream to it, but when I try to read in all my variables, they aren't being read. I know that my program is accessing the text file because I am testing it by doing this: if(leveldata.is_open()) { .... } Even though it accesses the file, the variables aren't being set, for example I have a mapwidth variable that , even though in the text file it says 100, is being set for 0 every time I load up the game. Can anyone help me with this? Here is a code snippet of what I am doing.
[source lang = "cpp"]
#include <fstream>
#include <iostream>

ifstream levdata (filename);
  {

  if(levdata.is_open())
  {



  levdata > mapname;
  levdata >> mapwidth;
  levdata >> mapheight;
  levdata >> tileset;
  levdata >> playerx;
  levdata >> playery;
  levdata >> timeofday;
  levdata >> numberofswitches;
  levdata >> numberofenemygroups;
  levdata >> numberofgenerators;
}

}



Thanks in advance.

Share this post


Link to post
Share on other sites
Sir Sapo, are you sure all your variables are in the same page and alterable by this function? You could do a printf and check what the variables are inside of the function itself. If they are correctly changed, then you may have problems regarding scope and stuff. You should keep everything in a class and pass the class name as reference to the function. Always the way to go.

Glad if I could help.

Share this post


Link to post
Share on other sites
I printf'd after a variable should have been initialized, but the value was still 0(its default), so I think it either has something to do with the textfile or something like that, I dont think scope is the problem here.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sir Sapo
Fixed it. I guess you can >> strings from a txt file. Thanks guys.


You can, but it will only read one "word" (i.e. up until the next whitespace, which is considered a delimiter between "items" by default when using formatted IO). To read a whole line, you probably want:


#include <string>
#include <fstream>
#include <iostream>

//...

std::string line; // or just use any existing variable of that type
std::getline(levdata, line);


Share this post


Link to post
Share on other sites
Its just me, but I find it easier to use the C-style file reading/writing for anything binary that I need to write. The insertion and extraction operators just get a bit hectic for me. I'm not saying that its bad to use, but that's just me! I'm also used to using that more than the latter.

Share this post


Link to post
Share on other sites
Quote:
Original post by bkt
Its just me, but I find it easier to use the C-style file reading/writing for anything binary that I need to write. The insertion and extraction operators just get a bit hectic for me. I'm not saying that its bad to use, but that's just me! I'm also used to using that more than the latter.

It's not difficult to write generic C++ functions to simplify reading and writing binary data with streams. Here's a version I quickly hacked together:

#include <fstream>
#include <iostream>
#include <string>

template < typename TYPE >
class BinaryHelper;

template < typename TYPE >
std::ostream & operator<<(std::ostream & stream, BinaryHelper< TYPE > const & value);

template < typename TYPE >
std::istream & operator>>(std::istream & stream, BinaryHelper< TYPE > const & value);

template < typename TYPE >
class BinaryHelper
{

public:

explicit BinaryHelper(TYPE & value);

private:

TYPE & value_;

friend std::ostream & operator<< <>(std::ostream & stream, BinaryHelper< TYPE > const & value);
friend std::istream & operator>> <>(std::istream & stream, BinaryHelper< TYPE > const & value);

};

template < typename TYPE >
BinaryHelper< TYPE >::BinaryHelper(TYPE & value)
:
value_(value)
{
}

template < typename TYPE >
BinaryHelper< TYPE > binary(TYPE & value)
{
return BinaryHelper< TYPE >(value);
}

template < typename TYPE >
std::ostream & operator<<(std::ostream & stream, BinaryHelper< TYPE > const & value)
{
stream.write(reinterpret_cast< char * >(&value.value_), sizeof(TYPE));
return stream;
}

template < typename TYPE >
std::istream & operator>>(std::istream & stream, BinaryHelper< TYPE > const & value)
{
stream.read(reinterpret_cast< char * >(&value.value_), sizeof(TYPE));
return stream;
}

struct PodType
{

PodType();
PodType(float floatValue_, int intValue_, double doubleValue_, char charArray_[4]);

float floatValue;
int intValue;
double doubleValue;
char charArray[4];

};

PodType::PodType()
:
floatValue(),
intValue(),
doubleValue()
{
std::strncpy(charArray, "", 4);
}

PodType::PodType(float floatValue_, int intValue_, double doubleValue_, char charArray_[4])
:
floatValue(floatValue_),
intValue(intValue_),
doubleValue(doubleValue_)
{
std::strncpy(charArray, charArray_, 4);
}

std::ostream & operator<<(std::ostream & stream, PodType const & podType)
{
stream << "PodType:\n\tfloatValue: " << podType.floatValue << "\n\tintValue: " << podType.intValue << "\n\tdoubleValue: " << podType.doubleValue << "\n\tcharArray: " << podType.charArray << '\n';
return stream;
}

void writeFile(std::string filename)
{
std::ofstream writer(filename.c_str());
int intValue = 707;
float floatValue = 7.07;
double doubleValue = 707.0707;
short shortValue = -7;
unsigned short unsignedShortValue = 7;
PodType podType(543.21, 987, 97531.248, "Hi!");
writer << intValue << ' ' << binary(intValue) << floatValue << ' ' << binary(floatValue) << doubleValue << ' ' << binary(doubleValue) << shortValue << ' ' << binary(shortValue) << unsignedShortValue << ' ' << binary(unsignedShortValue) << binary(podType);
}

void readFile(std::string filename)
{
std::ifstream reader(filename.c_str());
int intValue;
int intValueBinary;
float floatValue;
float floatValueBinary;
double doubleValue;
double doubleValueBinary;
short shortValue;
short shortValueBinary;
unsigned short unsignedShortValue;
unsigned short unsignedShortValueBinary;
PodType podTypeBinary;
reader >> intValue >> std::ws >> binary(intValueBinary) >> floatValue >> std::ws >> binary(floatValueBinary) >> doubleValue >> std::ws >> binary(doubleValueBinary) >> shortValue >> std::ws >> binary(shortValueBinary) >> unsignedShortValue >> std::ws >> binary(unsignedShortValueBinary) >> binary(podTypeBinary);
std::cout << "intValue: " << intValue << "\nfloatValue: " << floatValue << "\ndoubleValue: " << doubleValue << "\nshortValue: " << shortValue << "\nunsignedShortValue: " << unsignedShortValue << '\n';
std::cout << "intValueBinary: " << intValueBinary << "\nfloatValueBinary: " << floatValueBinary << "\ndoubleValueBinary: " << doubleValueBinary << "\nshortValueBinary: " << shortValueBinary << "\nunsignedShortValueBinary: " << unsignedShortValueBinary << '\n';
std::cout << "podTypeBinary:\n" << podTypeBinary;
}

int main()
{
std::string filename = "binaryFile.dat";
writeFile(filename);
readFile(filename);
}


Enigma

Share this post


Link to post
Share on other sites
That's actually very interesting, and I've never thought of doing that! I'm now considering writing that same type of wrapper for when I create my pakfile-like format. Hmm, thanks.

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