943,871 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2667
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 14th, 2009
0

Re: Problem with ifstream returning fail()

Thanks.

Everyone knows that differences and they could program accordingly.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 14th, 2009
0

Re: Problem with ifstream returning fail()

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 2:13 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,951 posts
since Aug 2005
Jun 14th, 2009
1

Re: Problem with ifstream returning fail()

The wittingly wrong statement in OP code is delete buffer; - must be delete[]buffer; . Yet another defect: insufficient error check-up.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 14th, 2009
0

Re: Problem with ifstream returning fail()

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:
c++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 31
Solved Threads: 1
Light Poster
CppFTW is offline Offline
41 posts
since Dec 2008
Jun 14th, 2009
0

Re: Problem with ifstream returning fail()

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:
C++ Syntax (Toggle Plain Text)
  1. if (!FileToString(...)) { // Haha, if NOT means SUCCESS
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 14th, 2009
0

Re: Problem with ifstream returning fail()

Okay.

Thanks all. I understand why I was getting my problem now and also was able to fix some more problems in my code.
Reputation Points: 31
Solved Threads: 1
Light Poster
CppFTW is offline Offline
41 posts
since Dec 2008
Jun 15th, 2009
0

Re: Problem with ifstream returning fail()

Click to Expand / Collapse  Quote originally posted by CppFTW ...
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
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 15th, 2009
0

Re: Problem with ifstream returning fail()

Okay, done.
Reputation Points: 31
Solved Threads: 1
Light Poster
CppFTW is offline Offline
41 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Program crashes question
Next Thread in C++ Forum Timeline: Creating 2 dimentional graphs





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC