ofstream

#include<iostream>
#include<string>
#include<conio.h>
#include<list>
#include<algorithm>
#include<fstream>

using namespace std;

Hi!!!
I have a problem with std::ofstream.
class ETR
{
private:
string Name;
ofstream out;
public:
ETR(string s)
{
Name=s;
out("FIELD.txt");
out<<Name<<"\n";
};
~ETR()
{
out.close();
};
void Add(string s)
{
out<<s<<"\n";
};
};
int main()
{
ETR *etr=new ETR("FIELD");
etr->Add("saefaaf");
etr->Add("dalahfafklhafkl");

};
Everything compiles well. But after nothing is written to file. Please tell me why?
Thanks for help.
Last edited on
[code] "Please use code tags" [/code]

Everything compiles well.
change your compiler then. out("FIELD.txt");error: no match for call to '(std::ofstream) (const char [10])'

nothing is written to file. Please tell me why?
I guess that is because of the memory leak. The destructor is not called and the stream is not flushed. ¿why did you use dynamic allocation?
I'am sorry for the first code. That was not orrect. But now it seems to be fine.
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
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<string>
#include<conio.h>
#include<list>
#include<algorithm>
#include<fstream>

using namespace std;

  class ETR
{
      private:
              string Name;
              ofstream out;
      public:
             ETR(string s)
             {
                        Name=s;
                        out.open("FIELD.txt");
                        out<<Name<<"\n";
                        };
             ~ETR()
             {
                out.close();
                };
             void Add(string s)
             {
                  out<<s<<"\n";
                  };
                  };
                  
int main()
{
    ETR *etr=new ETR("FIELD");
    etr->Add("saefaaf");
    etr->Add("dalahfafklhafkl");
    
};

It commpiles with no errors. But the FIELD.txt is still empty. What is wrong with this code. I cant figure it out.
I'll appriciate any help.
Thanks.
Last edited on
Topic archived. No new replies allowed.