[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]