• Advertisement
Sign in to follow this  

[C++]Fstream not creating file

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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.

Share this post


Link to post
Share on other sites
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
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
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
Sign in to follow this  

  • Advertisement