Results deleting. Any solution?

Hello.
I dont really understand why is my old results deleting from result file.
I think that function void deletes it but why?
Can someone explain why and how to solve?(How to make that files would be not deleted but added?)

Example:
1
2
    fr << "something written" << endl;
    Truk(VE, VA);//Its void functiom 

fr its output for (.txt) file
Last edited on
Example...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

using namespace std;

int main() {

	fstream fs;
	fs.open("test.txt", fstream::app); // the fstream::app flag here means append file not replace.

	if (fs.is_open()) {

		fs << "\nwords to add";
		fs.close();
	}

	else {
		cout << "error.";
	}

	return 0;
}
umm not really understood.
Here is my example of problem:
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
#include <fstream>
#include <iomanip>
using namespace std;
const char CRfv[] = "Result.txt";

void A();

int main ()
{
    ofstream fr(CRfv);
    fr << "hello" << " " << endl;
    A ();

    fr.close();

    return 0;
}
void A()
{
    ofstream fr(CRfv);
    fr << "Nope";
        fr.close();

}


Then you open Result.txt it will be written (Nope).
I need that in Result.txt would be (hello Nope).
First, you can pass the mode in either ctor or open():
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
1
2
3
4
5
fstream fs;
fs.open("test.txt", fstream::app);

// OR
fstream fs("test.txt", fstream::app);


However, accessing same file independently and simultaneously from multiple locations is asking for trouble for no gain.

Pass the stream around:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>
#include <iomanip>

void A( std::ofstream& out );

int main ()
{
    const char* CRfv = "Result.txt";
    std::ofstream fr( CRfv );
    fr << "hello" << " \n";
    A( fr );
    fr.close();
    return 0;
}

void A( std::ofstream& out )
{
    out << "Nope";
}
1
2
3
    ofstream fr(CRfv);
    fr << "hello" << " " << endl;
    A ();

You can't write on a file if you don't open it.

You should add this: fr.open("Result.txt") before you attempt to write anything on the file
He does open it:
(2) initialization constructor
Constructs an ofstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
Internally, its ostream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer). Then, filebuf::open is called with filename and mode as arguments.
If the file cannot be opened, the stream's failbit flag is set.
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
#include <fstream>
#include <iomanip>
using namespace std;
const char CRfv[] = "Result.txt";

void A(fstream &);

int main()
{
	fstream fr;
	fr.open(CRfv, fstream::app);
	fr << "hello" << " " << endl;
	A(fr);

	fr.close();

	return 0;
}

void A(fstream &fr)
{
	
	
	fr << "Nope";
	

}
Thanks guys!
@keskiverto

My bad
Topic archived. No new replies allowed.