Need help with fstream

Hello, the two files (save, save1) dont create themselves in the folder with cpp file, but save the integers, while console is opened.

So, why the files dont appear?

Part of the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  if (px=="save")  {
	
	ifstream save1("save1.txt");
	fstream save("save.txt");
	
	ad=b;
	save<<ad;
	cout<<"s\n"<<ad;
	
	save1.close(); 
        save.close(); }
  
    if (px=="load")  {
        	
	ifstream save1("save1.txt");
	fstream save("save.txt");
	
	save>>ad;
        b=ad;
	cout<<"l\n";
	
	save1.close(); 
        save.close();  }


Tell me if you need the full code.
Last edited on
You are using open to open the same file in save and save1. This is unnecessary anyway, as the constructors open the text files anyway. Also, you should make it a habit of checking to see if the text files have been successfully opened, and if not print an error message and quit (or something to that effect).
Well, after checking with

 
if (save.is_open()) cout<<"1";

it didnt show the number 1...
What is px?
What is save for?

Tell me if you need the full code.
Not the full code, but a minimal code that reproduces the problem.
px is string,
save is the .txt file in which i want to save ad ( integer )

The code that i gave has the problem,
which is that the 2 .txt files dont appear.
Last edited on
save is the .txt file in which i want to save ad ( integer )
ok, I meant save1, that is useless, not save.

In the save branch, you should use ofstream (or fstream with mode = std::ios_base::out).
Topic archived. No new replies allowed.