-
July 6th, 2013, 03:15 PM
#1
Passing fstream object to function
Hello.
I want my function to take 'fstream' object as an input, so the program looks like this:
Code:
#include <fstream>
using namespace std;
void test(fstream a){
a.open("test2.txt");
a << "123" << endl;
a.close();
}
int main(){
fstream a;
test(a);
return 0;
}
But I get the following error: 'std::ios_base::ios_base(const std::ios_base&)' is private|
Please help to make it work.
-
July 6th, 2013, 04:00 PM
#2
Re: Passing fstream object to function
Code:
#include <fstream>
using namespace std;
void test(fstream& a){
a.open("test2.txt");
a << "123" << endl;
a.close();
}
int main(){
fstream a;
test(a);
return 0;
}
pass fstream by ref rather than value. You can't pass a stream by value.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 6th, 2013, 04:08 PM
#3
Re: Passing fstream object to function
Thanks, it can be compiled now, but expected .txt file is not created. Where is a catch?
Never mind. It must be written ofstream. Thanks again.
Last edited by ted_kingdom; July 6th, 2013 at 04:12 PM.
-
July 6th, 2013, 04:25 PM
#4
Re: Passing fstream object to function
Originally Posted by ted_kingdom
Thanks, it can be compiled now, but expected .txt file is not created. Where is a catch?
Never mind. It must be written ofstream. Thanks again.
There is no catch. You are trying to open test2.txt. If the file does not exist, then the file is not created but an error occurs. You are not checking that the file opens successfully before writing to it.
Code:
void test(fstream& a){
a.open("test2.txt");
if (a.is_open()) {
a << "123" << endl;
a.close();
} else {
cout << "Cannot open test2.txt\n";
}
}
If you always want the file to be created if it does not exist, then use
Code:
a.open("test2.txt", fstream::out);
Note that in both cases, the existing contents (if any) of the file will be overwriten.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|