#include <iostream>
#include <string>
#include <fstream>
//
int main()
{
std::string str;
std::fstream file;
std::cin>>str;
file.open("pfffft.txt");
std::cout<<file.is_open();
file<<str;
file.close();
std::cout<<file.is_open();
std::cin.get();
std::cin.get();
return 0;
}
[C++]Fstream not creating file
Once again I've run into some stupid problem...
As far as I know this code should create create a file or if it's already created open it for input output operations. Then write user input into the file and write 10 on the screen.
Now, what it does is just take user input (not write into the file) and write 00.
The only exception to this is when i move .exe file out of compile destination and create pfffft.txt by myself...
So what could the problem be?
Oh, and this tiny program doesn't even open on my friend's PC, I'd appreciate some clever explanation on that too.
Thanks in advance.
For output use std::ofstream. For file input use std::ifstream. I guess std::fstream is for cases where you want to perform input and output at the same time (or you'll need to specify the open mode explicitly).
Quote:Original post by visitor
For output use std::ofstream. For file input use std::ifstream. I guess std::fstream is for cases where you want to perform input and output at the same time (or you'll need to specify the open mode explicitly).
It still should be able to use only output and the pfffft.txt should be created... Yet no signs of it...
Thanks for reply though
fstream::open
See what the default modes are for different streams. When you open a file for input, the file must exist or open will fail.
See what the default modes are for different streams. When you open a file for input, the file must exist or open will fail.
You are using the default constructor for the stream does this set the ios flags?
try the following or as suggested use an ofstream.
What are the permission on the directory you are trying to create the file in?
Edit:
That would explain it, ios_base::in and ios_base::out are set as the flags for a default fstream as it is in and out stream.
try the following or as suggested use an ofstream.
int main(){ std::string str; std::fstream file("pfffft.txt",std::ios_base::out | std::iso_base::trunc); if(file.fail()) throw std::runtime_error("file operation error"); std::cin>>str; file<<str;}
What are the permission on the directory you are trying to create the file in?
Edit:
Quote:When you open a file for input, the file must exist or open will fail.
That would explain it, ios_base::in and ios_base::out are set as the flags for a default fstream as it is in and out stream.
Strange that I didn't find anywhere that if fstream object is with ios_base::in | ios_base::out flags file won't be created. Thanks for pointing that out :)
Is there any way to use same fstream object for in/out and make sure that file gets created?
Is there any way to use same fstream object for in/out and make sure that file gets created?
Don't bother reusing the same file stream object. Use a std::ifstream and std::ofstream as necessary. Remember the KISS principle - Keep It Simple, StupidTM. [smile]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement