| | |
Problem with ifstream returning fail()
Thread Solved |
I just got back from work and looked at the zip file you posted.
The program fails when function dummy() calls FileToString() because the file name (first parameter) contains the '\n' at the end of the filename, so the os can't find that file.
To correct that you will have to modify function Tokenize() to strip the character from the string.
The program fails when function dummy() calls FileToString() because the file name (first parameter) contains the '\n' at the end of the filename, so the os can't find that file.
To correct that you will have to modify function Tokenize() to strip the character from the string.
vector<string> Tokenize(string data, char delimiter)
{
vector<string> pieces;
unsigned int a = 0;
unsigned int b;
while (true)
{
b = (unsigned int)(data.find(delimiter, a));
if (b == string::npos)
break;
pieces.push_back( data.substr(a, b - a-1));
a = b + 1;
}
pieces.push_back(data.substr(a));
return pieces;
} Last edited by Ancient Dragon; Jun 14th, 2009 at 1:13 am.
The wittingly wrong statement in OP code is
About wrong filelist parsing (adatapost's tip):
If you want a portable code, never use FileToString to load line-oriented filelist file. It's the case where binary (low-level) stream/string processing is an absolutely irrelevan method. The old good std::getline is the best and the portable tool to "tokenize" a simple text file!
By the way, never add non-inline function definitions in .h files! Have you ever heard about separate compilation? Only function prototypes are placed in .h files.
delete buffer; - must be delete[]buffer; . Yet another defect: insufficient error check-up. C++ Syntax (Toggle Plain Text)
long GetFileSize(ifstream &file) { static const streamoff badPos(-1); if (!file) // test added return -1; streamoff temp = file.tellg(); if (temp != badPos && file.seekg(0L, ios::end)) { streamoff end = file.tellg(); if (end != badPos && file.seekg(temp, ios::beg)) return static_cast<long>(end); } return -2; } /// Load the file to fileS string, return filesize. /// Non-positive returned if failed long FileToString(const string &file_path, string &fileS) { ifstream input(file_path.c_str(), ios::binary); if (!input) { // simple and clear stream state test fileS.clear(); // no contents return -1; } long size = GetFileSize(input); if (size <= 0) { // filesize test added. fileS.clear(); return -2; } char* buffer = new char[size]; if (input.read(buffer, size)) { fileS.reserve(size+size/10); // add reserve fileS.assign(buffer, size); } else { size = -3; } delete [] buffer; // [] inserted! //input.close(); // no need: let destructor works return size; }
If you want a portable code, never use FileToString to load line-oriented filelist file. It's the case where binary (low-level) stream/string processing is an absolutely irrelevan method. The old good std::getline is the best and the portable tool to "tokenize" a simple text file!
By the way, never add non-inline function definitions in .h files! Have you ever heard about separate compilation? Only function prototypes are placed in .h files.
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 0
First, sorry for the late response. I was sleeping and forgot to mention it in the last post.
@Ancient Dragon: I kind of get what you are saying because the file names do contain things like '\n' or '\r' in them, but your change to the Tokenize function breaks its generic functionality. This code doesn't work correctly:
But, I get what you mean by having to take those \r or \n out of my file names.
@ArkM: Thanks for going through my code. But, I am a bit confused about some of your fixes:
1. At line 31, why do I need to reserve
2. For
Thanks all. I "approve" of all your posts :D (reputation)
@Ancient Dragon: I kind of get what you are saying because the file names do contain things like '\n' or '\r' in them, but your change to the Tokenize function breaks its generic functionality. This code doesn't work correctly:
c++ Syntax (Toggle Plain Text)
int main() { vector<string> temp = Tokenize("blah;blah;blah;blah;", ';'); for (int i = 0; i < temp.size(); i++) { cout << temp[i] << endl; } return 0; }
@ArkM: Thanks for going through my code. But, I am a bit confused about some of your fixes:
1. At line 31, why do I need to reserve
size + size/10 ? Shouldn't it just be size ?2. For
FileToString , I was thinking of just returning the error code and not the file size because I can just do fileS.size() . Is this a bad programming habit on my part?Thanks all. I "approve" of all your posts :D (reputation)
1. Well, reserve size (if no need in some postprocessing)
2. Tastes differs; for example, I prefer to return bool in such cases. I think, returned size if more useful than 0. Moreover, zero on success is not a natural code, look at:
2. Tastes differs; for example, I prefer to return bool in such cases. I think, returned size if more useful than 0. Moreover, zero on success is not a natural code, look at:
C++ Syntax (Toggle Plain Text)
if (!FileToString(...)) { // Haha, if NOT means SUCCESS
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Problem with ifstream::clear() (C++)
- Problem related to c in looping (C)
- Client side code for a Web Method Returning a complex Custom data type. (RSS, Web Services and SOAP)
- problem: cfquery returning scientific notation (ColdFusion)
- Reading Int/Double from a .txt File (C++)
- DataGridView integer problem? (VB.NET)
- inputing a text file problem with ifstream (C++)
- I need help with languages (PHP)
Other Threads in the C++ Forum
- Previous Thread: Program crashes question
- Next Thread: Creating 2 dimentional graphs
Views: 1488 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int integer lazy link linker list loop loops map math matrix member memory network newbie number object objects opengl operator output parameter pointer pointers problem program programming project qt random read reading recursion recursive reference return server sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock wxwidgets






