fstream inside of class

Greetings. I have several classes that manipulate certain files, and each of these classes has an fstream as one of the variables. Here are boiled-down versions of some definition and implementation files:

FileX.h
-------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

class FileX {
   public:
      FileX();
      FileX(int Param1, int Param2);

      ~FileX();

   private:
      fstream F_X;
};


FileX.cpp
---------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "FileX.h"

//----------------------------------------------------------

FileX::FileX()
{
}

//----------------------------------------------------------

FileX::FileX(int Param1, int Param2)
{
   char fname[7];

   sprintf(fname, "X%02d.%02d", Param1, Param2);
   F_X.open(fname, ios::binary | ios::in | ios::out);
}

//----------------------------------------------------------

FileX::~FileX()
{
   F_X.close();
}


FileY.h
-------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

#include "FileX.h"

class FileY {
   public:
      FileY();
      FileY(int Param1, int Param2, FileX FX_t);

      ~FileY();

   private:
      fstream F_Y;

      FileX FileX_t;
};


FileY.cpp
---------
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
#include "FileY.h"

//----------------------------------------------------------

FileY::FileY()
{
}

//----------------------------------------------------------

FileY::FileY(int Param1, int Param2, FileX FX_t)
{
   char fname[7];

   sprintf(fname, "Y%02d.%02d", Param1, Param2);
   F_Y.open(fname, ios::binary | ios::in | ios::out);

   FileX_t = FX_t;
}

//----------------------------------------------------------

FileY::~FileY()
{
   F_Y.close();
}



The command g++ -c FileX.cpp works fine; however, the command
g++ -c FileY.cpp gives the following error:

FileY.cpp: In member function `std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/ios_base.h:784: error: `std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private
FileY.cpp:18: error: within this context
FileY.cpp: In member function `std::basic_filebuf<char, std::char_traits<char> >& std::basic_filebuf<char, std::char_traits<char> >::operator=(const std::basic_filebuf<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/streambuf:776: error: `std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
FileY.cpp:18: error: within this context


It appears to me that the problem is with the copy of the fstream part of the FileX object. Does anyone have suggestions as to how to fix this problem? Thanks.
fstream objects are not copyable (or assignable) but you are attempting to assign it.

FileX_t = FX_t;
This line makes a copy of the FX_t and, in the process, attempts to copy FileX::F_X. The copy constructor for fstream is private.
Thanks for your replies. Now I have a follow-up question. I have decided to have an fstream* instead of an fstream in FileX, as shown below. I added an fstream& argument to the constructor, which I intend to have set a file name, open a file, and set a pointer to this fstream. I have added two more functions, one which is supposed to write the number 5 to the file and one which is supposed to read it. The program compiles, but when it is run it does not create the file. Here is
the code:

File a.cpp
------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

#include "FileX.h"

int main()
{
   fstream ff;

   FileX fx(5,2,ff);

   fx.fw();
   fx.fr();
}



File FileX.h
--------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

class FileX {
   public:
      FileX();
      FileX(int Param1, int Param2, fstream& ff);

      void fr();
      void fw();

      ~FileX();

   private:
      fstream* F_X;
};



File FileX.cpp
----------------
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
39
40
41
42
43
44
45
46
#include "FileX.h"

//----------------------------------------------------------

void FileX::fr()
{
   int i = -8;

   (*F_X).seekg(0, ios::beg);
   (*F_X).read((char *) &i, sizeof(int));
   cout << i << endl;
}

//----------------------------------------------------------

void FileX::fw()
{
   int i = 5;

   (*F_X).write((char *) &i, sizeof(int));
}

//----------------------------------------------------------

FileX::FileX()
{
}

//----------------------------------------------------------

FileX::FileX(int Param1, int Param2, fstream& ff)
{
   char fname[7];

   sprintf(fname, "X%02d.%02d", Param1, Param2);
   ff.open(fname, ios::binary | ios::in | ios::out);

   F_X = &ff;
}

//----------------------------------------------------------

FileX::~FileX()
{
   (*F_X).close();
}



The commands

g++ -c FileX.cpp
g++ a.cpp FileX.o
a.out

produce the output

-8


How do I fix this?
I figured it out. The open statement should be replaced by the following:

1
2
3
   ff.open(fname, ios::binary | ios::out);
   ff.close();
   ff.open(fname, ios::binary | ios::in | ios::out);


The first open statement creates the file if it doesn't exist. The second open statement does not.
Topic archived. No new replies allowed.