Problem with ifstream returning fail()

Thread Solved

Join Date: Oct 2008
Posts: 3,926
Reputation: adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future 
Solved Threads: 704
Moderator
adatapost adatapost is offline Offline
Senior Poster

Re: Problem with ifstream returning fail()

 
0
  #11
Jun 14th, 2009
Thanks.

Everyone knows that differences and they could program accordingly.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,601
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Problem with ifstream returning fail()

 
0
  #12
Jun 14th, 2009
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with ifstream returning fail()

 
1
  #13
Jun 14th, 2009
The wittingly wrong statement in OP code is delete buffer; - must be delete[]buffer; . Yet another defect: insufficient error check-up.
  1. long GetFileSize(ifstream &file)
  2. {
  3. static const streamoff badPos(-1);
  4. if (!file) // test added
  5. return -1;
  6. streamoff temp = file.tellg();
  7. if (temp != badPos && file.seekg(0L, ios::end)) {
  8. streamoff end = file.tellg();
  9. if (end != badPos && file.seekg(temp, ios::beg))
  10. return static_cast<long>(end);
  11. }
  12. return -2;
  13. }
  14.  
  15. /// Load the file to fileS string, return filesize.
  16. /// Non-positive returned if failed
  17. long FileToString(const string &file_path, string &fileS)
  18. {
  19. ifstream input(file_path.c_str(), ios::binary);
  20. if (!input) { // simple and clear stream state test
  21. fileS.clear(); // no contents
  22. return -1;
  23. }
  24. long size = GetFileSize(input);
  25. if (size <= 0) { // filesize test added.
  26. fileS.clear();
  27. return -2;
  28. }
  29. char* buffer = new char[size];
  30. if (input.read(buffer, size)) {
  31. fileS.reserve(size+size/10); // add reserve
  32. fileS.assign(buffer, size);
  33. } else {
  34. size = -3;
  35. }
  36. delete [] buffer; // [] inserted!
  37. //input.close(); // no need: let destructor works
  38. return size;
  39. }
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: CppFTW is an unknown quantity at this point 
Solved Threads: 0
CppFTW CppFTW is offline Offline
Newbie Poster

Re: Problem with ifstream returning fail()

 
0
  #14
Jun 14th, 2009
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:
  1. int main()
  2. {
  3.  
  4. vector<string> temp = Tokenize("blah;blah;blah;blah;", ';');
  5. for (int i = 0; i < temp.size(); i++)
  6. {
  7. cout << temp[i] << endl;
  8. }
  9.  
  10. return 0;
  11. }
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 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)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with ifstream returning fail()

 
0
  #15
Jun 14th, 2009
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:
  1. if (!FileToString(...)) { // Haha, if NOT means SUCCESS
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: CppFTW is an unknown quantity at this point 
Solved Threads: 0
CppFTW CppFTW is offline Offline
Newbie Poster

Re: Problem with ifstream returning fail()

 
0
  #16
Jun 14th, 2009
Okay.

Thanks all. I understand why I was getting my problem now and also was able to fix some more problems in my code.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with ifstream returning fail()

 
0
  #17
Jun 15th, 2009
Originally Posted by CppFTW View Post
Okay.

Thanks all. I understand why I was getting my problem now and also was able to fix some more problems in my code.
If so, mark this thread as solved
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: CppFTW is an unknown quantity at this point 
Solved Threads: 0
CppFTW CppFTW is offline Offline
Newbie Poster

Re: Problem with ifstream returning fail()

 
0
  #18
Jun 15th, 2009
Okay, done.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1488 | Replies: 17
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC