Sign in to follow this  
Seoushi

odd string issue, possible bad file loading.

Recommended Posts

So I want to load some config files for my engine that I'm creating, but for some reason I get some odd escape character or an unterminated string or something werid to that effect. Here is the code in question bool loadTileset(string filename) { string file = "data/tilesets/" + filename + ".tst"; cout << "Loading tileset " << file << endl; cFile tstFile; if( tstFile.load(file) == false ) { return false; } ... more stuff here } this code generates this text on the console: .tsting tileset data/tilesets/village Which makes it look like the filename string makes it so everything printed after it start over at the begining of the line and overwrites whatever is there. When I set filename to something at the begining of the function it works just fine however. So that leads me back to the filename string is being sent in wrong and further more this filename is read from a file so it makes me believe that I'm reading that file wrong. here is the code calling the loadTitleset mapFile.nextLine(); if(loadTileset( mapFile.getValue() ) == false){ return false; } now your probley wondering how I'm loading a file and what the heck the "cFile" class is. The cFile class isn't huge and its very straight forward so I'm just gonna post it. cFile.h class cFile { public: cFile(); ~cFile(); bool load(std::string filename); void close(); std::string getValue(); std::string getLine(); int getIntValue(); void nextLine(); std::string delim; private: std::ifstream file; std::string currentLine; }; cFile.cpp #include "globals.h" #include "cFile.h" using namespace std; cFile::cFile() { delim = '='; } cFile::~cFile() { if(file.is_open()) { file.close(); } } void cFile::close() { file.close(); } bool cFile::load(string filename) { file.open(filename.c_str()); if(file.is_open()){ return true; } else{ return false; } } string cFile::getValue() { string str = currentLine; unsigned int pos = str.find(delim, 0); if (pos != string::npos) { str = str.substr(pos + 1, str.length() - pos); return str; } return str; } int cFile::getIntValue() { string str = currentLine; unsigned int pos = str.find(delim, 0); if (pos != string::npos) { str = str.substr(pos + 1, str.length() - pos + 1); atoi(str.c_str()); } return atoi(str.c_str()); } string cFile::getLine() { return currentLine; } void cFile::nextLine() { getline(file, currentLine); } To make the matter even stranger this code works perfectly fine on windows but not on linux. This leads me be believe that linux is picking up some odd escape character or windows handles some error I made better than linux. any ideas?

Share this post


Link to post
Share on other sites
Quote:

To make the matter even stranger this code works perfectly fine on windows but not on linux. This leads me be believe that linux is picking up some odd escape character or windows handles some error I made better than linux.

I haven't read the whole post, but for your information text files on Linux and Windows are different -- Linux ends its lines with '\n' while Windows ends its lines with '\r\n'.

Greetz,

Illco

Share this post


Link to post
Share on other sites
The problem is that your filename variable contains an extra \r character at the end. This is causing "file" to contain "data/tilesets/village\r.tst", which in turn causes the rewinding to the beginning of the line that you see.

As for getline and the \r character: "delim" is "\n" by default, and although delim isn't included in the returned string, \r can be. Dealing with this is a binary/text mode issue... seeing as you're allready in text mode, i have to ask: Are you opening files from windows in linux? If so, this would be the probable cause of the problem - only the system newlines are translated into \n IIRC (aka, on windows, it translates \r\n into \n in text mode, whereas on linux only \n translates into \n).

Two solutions:

1) Trim off the \r yourself.

2) Use an newline conversion program such as:
2.1) For windows: dos2lin or lin2dos (depending on direction)
2.2) For linux: fromdos or todos (for Debian this is in the sysutils package)

Final notes:
SDL is a C multimedia/rendering library, with nothing to do with getline.
STL may have been what you meant, standing for the Standard Template Library.
The STL is now officially called the Standard C++ Library. Plenty of people still refer to it as the STL, however, since it's a whole lot shorter to type.
Personally, I now prefer "std::" or "libstdc++" (the .so library name on my linux box) for shorthand, because I'm crazy like that.

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