Fstream may be addicted to crack!

Started by
5 comments, last by xorjesus 20 years ago
[qoute] Stats: MS Dev Studio 6.0 Enterprise Edition Project: Win 32 Console Mode Annoyance Level: 5 out of 5 I'm getting the most bizzare behaivor from using fstream in a specific way. The output i'm getting is incomprehensible to me. I'm hoping one of you gurus can enlighten me to why this program is acting this way. I have some example code that asks a user to enter a filename, and if the file exists, the program simply copies the first line using the getline function, and than outputs it back. Though, weird, horrible, scccaarry!<- problems arise based on a few benign changes or how the user enters his or her input. e.g. If I ask the user to enter a filename, and the user enters a proper filename, the program will output to the console with an empty line above and below the output line, weird I didn't specify anything like this. If the the user enters a wrong filename, than enters the right one, different output will happen. hmm ok. With a modification of one line to the program, different outputs can happen. Its like i'm working with a compiler that has output simlier to julia sets. Obviously a pattern, but the butterfly effect is in full proportion. I'll give two different sources to show this cracked out fstream. The first which relates to the examples.

/**************************************************************************
*text.txt															      *
*foooooooooooooooooooooooooooooooobaaaaaaaaaaaaaaarrrrrrrrrrrrrrrrrrrrrrrr*
***************************************************************************/
#include <iostream>
#include <fstream>

using namespace std;

void a(fstream &f_io)
{
	char test[255]; // Yes this is part of the problem, but the question

	                // is, why does it have such a large effect on the program,

	                // when the preceeding two lines should nullify its action.

	f_io.getline(test, 255, '\n');
	f_io.seekg(0, ios::beg);
	f_io.clear();
}

void main()
{
	char strTemp[255];
	char fileName[255];
	fstream f_io;

	while (1)
	{
		cout << "Enter filename: ";
		cin.getline(fileName, 255, '\n');
		
		f_io.open(fileName, ios::in);

		if (!f_io.is_open())
		{
			continue;
		}
		else 
			break;
	}
	
	//cout << endl;


	f_io.getline(strTemp, 255, '\n');
	cout << strTemp << endl; 
	
	//a(f_io);

	
	
	//f_io.getline(strTemp, 255, '\n');

	//cout << strTemp << endl;

}
   
//program snapshot:

Enter a filename: testtttt.txt;
Enter a filename: test.txt;
/////////////////////////////////////////

fooooooooooobbbbbbbbbaaaaaaaaarrrrrrrrrrr
Press Any Key to Exit

//2nd program snapshot

Enter a filename: test.txt
/////////////////////////////////////////

foooooooooobbbbbbbbbbaaaaaaaarrrrrrrrrrrr
/////////////////////////////////////////

Press Any Key to Exit
Here's another problem, this program will print 2 lines, which should be expected. Though if the user messes up with the filename input, the program only prints oneline, which is agrees with my hypothesis that fstream is on crack! The reason, i'm upset, is cuase i use a similier scheme in a program hundreds of lines long. I get even worse output, cuase the program is much more complex. I hope someone can tell me whats going on, and end this confusion! Ugh, thanks in advance - xorjesus
            
/**************************************************************************
*text.txt															      *
*foooooooooooooooooooooooooooooooobaaaaaaaaaaaaaaarrrrrrrrrrrrrrrrrrrrrrrr*
***************************************************************************/
#include <iostream>
#include <fstream>

using namespace std;

void a(fstream &f_io)
{
	char test[255]; // Yes this is part of the problem, but the question

	                // is, why does it have such a large effect on the program,

	                // when the preceeding two lines should nullify its action

	f_io.getline(test, 255, '\n');
	cout << test << endl;
	f_io.seekg(0, ios::beg);
	f_io.clear();
}

void main()
{
	fstream f_io;
	char fileName[255];
	char strTemp[255];
	while (1)
	{
		cout << "Enter filename: ";
		cin.getline(fileName, 255, '\n');
		
		f_io.open(fileName, ios::in);

		if (!f_io.is_open())
		{
			continue;
		}
		else 
			break;
	}
	

	
	a(f_io);
	
	
	f_io.getline(strTemp, 255, '\n');
	cout << strTemp << endl;
}
   
//program snapshot:

Enter a filename: testtttt.txt
Enter a filename: test.txt;
/////////////////////////////////////////

fooooooooooobbbbbbbbbaaaaaaaaarrrrrrrrrrr
/////////////////////////////////////////

Press Any Key to Exit

//2nd program snapshot

Enter a filename: test.txt
/////////////////////////////////////////

foooooooooobbbbbbbbbbaaaaaaaarrrrrrrrrrrr
fooooooooooobbbbbbbbbaaaaaaaaarrrrrrrrrrr
Press Any Key to Exit
Do you see how the output is different based on the user getting the filename correct, the only statement I use if they get the filename incorrect is continue, I don't know why it would change the output... [edited by - xorjesus on April 4, 2004 5:04:49 PM] [edited by - xorjesus on April 4, 2004 5:10:10 PM] [edited by - xorjesus on April 4, 2004 5:11:58 PM] [edited by - xorjesus on April 4, 2004 5:13:14 PM]
I study day and night, memorizing the game.
Advertisement
Could you give some sample output? You are very vague about what actually happens. What compiler are you using? Do you have the latest version, with the most up to date libraries?

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Ok i''ve posted some stats and output. I hope this helps, it sure would be fantastic to find out while those two short programs are behaving that way.
I study day and night, memorizing the game.
There''s a known bug in basic_istream::getline() with MSVC 6. Try applying the fix at this page and see if it solves your problem.
That bug affects version 5.0, 6.0 has its own fix to istream::getline(), so that''s not the problem :[
I study day and night, memorizing the game.
You say "Here''s another problem, this program will print 2 lines, which should be expected. Though if the user messes up with the filename input, the program only prints oneline, which is agrees with my hypothesis that fstream is on crack!"

Yet your 2nd example clearly shows 3 lines of text printed in both cases. It''s still confusing to see what you expect and how it relates to what you get.

To be honest though, I can''t see an obvious bug. But this is a short enough program to run under the debugger. Why not just step through it and see what''s going on? I''d be watching those char arrays closely, and also checking return values.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
By program printing two lines, I mean the output other than cout which askes for user input. I haven''t throughly debugged the program, but i''ll give it a few good hours of my time. I was just hoping someone could see an outright bug and explain it to me. Thanks for the suggestion and tryign to help
I study day and night, memorizing the game.

This topic is closed to new replies.

Advertisement