I have a function that compresses string and one function reading textfiles to strings. There's some issue that I can't nail. The idea is to read a textfile line by line and put each line into a string vector. When I dump the vector each line seems to be propper but when I try to use the vector in an function I get segmentation fault. Adding to my confusion - if I hard code the strings insterad of using file2string str2comp works.
The two functions are as follows.
Code:string str2comp(std::string str, int offset = 0){ ........string compstr; ........string::iterator it; ........int index = offset; ..for ( it = str.begin() ; it < str.end(); ++it){ ................if (index%4 == 0){ .. ..................compstr += (char)map.find(str.substr(index,4))->second; .... ........} ....................index++; .... } ........return compstr; }
I'm kind of green when it comes to C++ and I would really appreciate feedback or help.Code:string file2String(const string &fileName){ ....ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate); ....if(!ifs) .... ..{ .... .. ..cerr << "Error : file2String() could not open file: " << fileName << endl; .... .. ..exit(1); .... ..} else { .... ..ifstream::pos_type fileSize = ifs.tellg(); .... ..ifs.seekg(0, ios::beg); .... ..vector<char> bytes(fileSize); .... ..ifs.read(&bytes[0], fileSize); .... ..ifs.close(); .... ..return string(&bytes[0], fileSize); ....} }



3Likes
LinkBack URL
About LinkBacks





I used to be an adventurer like you... then I took an arrow to the knee.