Sign in to follow this  
Gylis

[C++]Fstream not creating file

Recommended Posts

Gylis    134
Once again I've run into some stupid problem...
#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;
}


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.

Share this post


Link to post
Share on other sites
visitor    643
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).

Share this post


Link to post
Share on other sites
Gylis    134
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

Share this post


Link to post
Share on other sites
CmpDev    100
You are using the default constructor for the stream does this set the ios flags?

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.

Share this post


Link to post
Share on other sites
Gylis    134
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?

Share this post


Link to post
Share on other sites
rip-off    10979
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this