declaring streams in header and cpp file

I am trying to declare a 2 streams, 1 for input and one for output, I know how to do this normally, but I am trying to do it now in a class, and over a header and cpp file which I have never done and I can not seem to figure it out, and I am getting errors with it.

The errors seem to be in the header file and is complaining about a type specifier and something about a dummy = int not being a type name. But this is how I normally declare the streams when not normally done in a class, should this be done differently now that it is in a class?

I was thinking I should maybe declare the header as myOutStream("string" ) but I am not sure what the ios::app should be, I read that a flag is a bool but when i tried myOutStream("string" , bool) it still does not work. Could someone please point me in the right direction.

header
1
2
3
4
5
6
7
8
class book
{
private:
	ofstream myOutStream("Books.txt", ios::app);
	ifstream myInStream("Books.txt");
}



cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void book::writeToFile(book const& obj)
{
	myOutStream("Books.txt", ios::app);
	if (!myOutStream)
	{
		cout << "Error opening file\n";
	}


}

void book::readFromFile()
{
	myInStream("Books.txt");


}
the constructor for ofstream is defined:
ofstream (const char* filename, ios_base::openmode mode = ios_base::out);

but you may not even need it .. is it always append? You can hard code it unless it needs to vary.

1
2
3
4
5
6
7
8
9
10
11
12
class book
{
private:
	ofstream myOutStream;
	ifstream myInStream;
    
        void openoutfile(string filename)
            {
                myOutStream.open(filename, ios::app); 
                etc....
            }
};



Last edited on
So far I think it will always be append. (Havent though too far ahead)

When i tried it this way.

1
2
ofstream myOutStream;
	ifstream myInStream;


I get errors because the arguments are different between the header file and the cpp file for the two functions. Also getting an E error code about the two functions saying "call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type"
Use braces, not parentheses, e.g.:
ofstream myOutStream{"Books.txt", ios::app};
If I remember correctly, parentheses can't be allowed because it would produce ambiguities in C++'s formal grammar (i.e., they're banned for no important reason).

The code has a classic race condition. It is not guaranteed that the same file will be opened twice.
https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use

Most likely you want to open the file once for both reading and writing:
std::fstream fs{"books.txt", std::ios_base::in | std::ios_base::out};

This being said, it seems a little odd for a book to incorporate a file stream.
Last edited on
to clarify:
using the {} locks you to the one file name. Is the name always the same?

myOutStream("Books.txt", ios::app); //I thought you wanted a METHOD because of this line in the cpp file and your comments, so that is what I gave you.

I see now you wanted a VARIABLE, which is what Mbozzi gave you.

Last edited on
Topic archived. No new replies allowed.