Archived

This topic is now archived and is closed to further replies.

I need some help on stream questions

This topic is 5132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been programming my own console games for quite sometime and recently have been trying find ways to save player data and hi scores. In my quest i''ve hit road block after road block. C++ streams just don''t seem logical to me! I have several questions which I am really hoping somebody can help me out with. OK lets says I have a text file called test.dat, and it has "hello world" saved in it. Regardless of binary or text mode, if I do a infile.get(dummy) loop and print the information to the screen, the last character is always echoed!! For example.
ifstream infile("test.dat");
char     dummy;

while(!infile.eof()) // why are there problems!!!!!!
{
   infile.get(dummy);
   cout << dummy;
}
cout << endl;
 
This would print out "Hello Worldd"; Now this is extremely annoying, and i''m not sure why it does this. Ok my 2nd question. This involves testing to see if a file exists, and if it doesn''t, then creating the file, write some random garbage, and then attempt to use the input stream on the file again. The problem is the input stream just wont work! If I have this code.
	ifstream infile;
	ofstream outfile;
	char dummy;
	infile.open("test2.dat"); // This file doesn''t exist
	
	// test to see if file exists.
	if (!infile)
	{
		infile.close();
		outfile.open("test2.dat");
		outfile << "Hello";
		outfile.close();
	}
	
	// we know it exists now!
	infile.open("test2.dat");

	for (int i=0; i<5; i++)
	{
		infile.get(dummy); // its taking in weird crap, it doesn''t print the string at all! Binary, or ascii, its not right.
		cout << dummy;
	}
	cout << endl;
 
This will print some random garbage, not "Hello". My third question is about streams being used in functions. A function here takes in a outfile stream as a parameter. When it writes, it creates the document but doesn''t save any of the text. Why is this?
int WriteSomething(ofstream outfile)
{
	//ofstream outfile;
	outfile.open("test3.dat");

	outfile << "I Know your not gonna save what I write.";
	outfile.close();

	return 1;
}

int main()
{
	ofstream outfile;
	WriteSomething(outfile);

	return 0;
}[ 
I know how to solve all of the problems, but that doesn''t mean I know why these solutions or problems exist. To solve #1, I use ifstream test stream. (ifstream test("test.data); if(!test){}) I haven''t been able to solve #2. In question #3 I solve it by referencing the stream, but that still doesn''t tell me why it originally didn''t work. Thank you so much for anybodies help.

Share this post


Link to post
Share on other sites
For the first example I think that it would be a bug. I assume that you are using visual c++ because when I run the code with visual c++ I get the same error. The code itself should be fine. (an example I have in a programming book is exactly the same except for a few changes in the variable names)

One way you could avoid the problem in the second example is to create a new instance of ifstream after you check for the file and load test2.dat into it. It seems that for ifstream to properly initialize and load the file you need to create a new instance of it (if the file wasn't loaded in the first open perhaps?).

The problem with the third example is probably due to some data not being copied over from the original ofstream to the one in the function. Its always better to use pointers and reference to the original ofstream so that you can be sure that you are dealing with the exact same data (in this case or any other case where you have an object containing data that you need to pass to a function).

[edited by - CorryGT on November 27, 2003 3:03:28 AM]

Share this post


Link to post
Share on other sites
First question:

I believe it is because the ifstream::eof method returns true only when pointer is past the end-of-file. Thus you do a last read and print it. Try fixing the problem by having a test statement -after- the read from file, like this.

while(!infile.eof))
{
infile.get(dummy);
if(!infile.eof())
cout << dummy;
}

For problem number two, try specifying which format you want to want to read in. Pass it as arguments to the constructor (logically OR them if you need to combine them). The constants are: ios_base::app (append), ios_base::ate (open at end of file), ios_base::binary (open as binary file), ios_base::in (allows in operations, meaning writing), ios_base::out (allows reading) and ios_base::trunc (truncates opened file to 0 bytes).

In your case, when you open test2.dat and write Hello to it, make sure you specify it like this outfile("test2.dat", ios_base::in | ios_base::trunc);

Then when you open it with infile again do it like this
infile("test2.dat", ios_base::out);

ALSO a very important issue, when you read from it cout doesn't know which format you desire, thus you must convert the stream like this:

cout << char(dummy);

Also for question number three. First of all the function is pretty worthless. Why do you pass a ofstream object that you anyway open in the function? Instead open it OUTSIDE the function and pass it by reference, so that the function only prints something. The problem is probably with the default constructor of the object since you pass by value and the object gets copied into outfile.

[edited by - biovenger on November 27, 2003 5:34:44 AM]

Share this post


Link to post
Share on other sites