C++ File Input/Output Problem

The purpose of the program is written at the top in the comment statements. My problem is with the second half, with the file output. I have tried the file input part by itself, and it works. I have commented that out, and tried the file output part by itself, and it works. But, when I put the two together, the program will not read/write to the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* Joshua McCullough
   3/11/10
   Programming Exercise 12.8 - This program will use data files to
                               track how many times a program has 
			       been executed.
*/
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	int counter;

	fstream inout;

	inout.open("Exercise12_8.dat", ios::in);
	inout >> counter;
	inout.close();
	
	inout.open("Exercise12_8.dat", ios::out);
	inout << counter++;
	inout.close();


	return 0;
}
Try clearing the file stream between the first close/second open.
Last edited on
Sorry, but we have not been taught how to clear the stream. How do I do this?
Clearing does the job, as firedraco said. But you still have to change line 22 into

 
inout << ++counter;


Maikel


EDIT:

1
2
3
4
	inout.open("Exercise12_8.dat", ios::in);
	inout >> counter;
	inout.close();
	inout.clear(); // reset all flags. 
Last edited on
Ah, yeah, you do need ++counter there too.

To clear the stream, just call the clear member function:
inout.clear();
I am sorry for asking the dumb question, I used VS Intellisense to show me how to use inout.clear();

So I did this, and changed counter++ to ++counter; and NOW IT WORKS.

Thank You Very Much firedraco and maikel!!!
Topic archived. No new replies allowed.