std::fstream doesn't create file - c++

I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
std::cout<<"error"<<strerror(errorno);
I get this result:
"No such file or directory."
How can I create the file in this case?

You're specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file.
Either remove std::fstream::in from your mode argument, or specify std::fstream::trunc in addition to the other flags.

It's a little messy but works. Doesn't overwrite the file if it exists but creates a new one if the first open fails.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
{
my_stream.open("my_file_name",std::fstream::binary | std::fstream::trunc | std::fstream::out);
my_stream.close();
// re-open with original flags
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
}
else
{
// read something
}
// read/write here

Related

File object with fstream object is not created

I am trying to create a binary file as follows:
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Hello World";
fstream fileObj = std::fstream("test_File.db", std::ios::in | std::ios::out | std::ios::binary);
if(fileObj)
std::cout<<"success";
else
std::cout<<"fail";
return 0;
}
But fileObj is not created and always else part is executed. Please guide if I am missing anything.
A stream opened with in | out | binary does not create a file that does not exist. You should get into the habit of reading the documentation!
Try in | out | app | binary (assuming you want existing contents to be kept; also get into the habit of clearly stating your goal/requirements).
And there is no need to initialise from a temporary like that; just instantiate the object in the usual manner, e.g.
std::fstream fileObj(
"test_File.db",
std::ios::in | std::ios::out | std::ios::app | std::ios::binary
);

Can't open a new in and out file with fstream

I'd like to open a new readable and writable file with fstream. I tried to do like the code below, but it failed because the file didn't exist.
file.open(file_path, ios::in | ios::out | ios::binary);
Is there any good way to do this with a new file?
I wrote the code below but it seemed ugly.
file.open(file_path, ios::out | ios::binary);
file.close();
file.open(file_path, ios::in | ios::out | ios::binary);
If you want to overwrite file that already exists you should use:
file.open(file_path, ios::in | ios::out | ios::binary | ios::trunc);
Appending to such a file:
file.open(file_path, ios::in | ios::out | ios::binary | ios::app);

Having trouble outputting file [duplicate]

I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
std::cout<<"error"<<strerror(errorno);
I get this result:
"No such file or directory."
How can I create the file in this case?
You're specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file.
Either remove std::fstream::in from your mode argument, or specify std::fstream::trunc in addition to the other flags.
It's a little messy but works. Doesn't overwrite the file if it exists but creates a new one if the first open fails.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
{
my_stream.open("my_file_name",std::fstream::binary | std::fstream::trunc | std::fstream::out);
my_stream.close();
// re-open with original flags
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
}
else
{
// read something
}
// read/write here

Can't write/read with fstream

When i write like this
std::fstream file ("data.dat", std::ios::in | std::ios::out);
if (!file)
{
std::cerr << "Error";
exit(1);
}
I receive "Error". When i rewrite the whole thing like this
std::fstream file;
file.open("data.dat", std::ios::out | std::ios::in);
if (!file)
{
std::cerr << "Error";
exit(1);
}
data.dat is being created, and i manage to write and read. Am doing this on VS 2013, in addition to that i tried compiling on 6.0 version and both ways have worked. I have no clue what the problem is.
You get the error because you instructed fstream to open the file for reading as well. If the file does not exist flags in | out will give you an error. Try it with only out and it should work. If you want to do write and read on a file that does not exist you need an extra flag, either append (app) or truncate (trunc)

How to set filename parameter as actual filename

I'm wondering how I can set a filename which my function receives as a parameter set as an actual filename.
My Syntax is
std::ofstream file("filename.bin",
std::ios::out | std::ios::trunc | std::ios::binary);
But that will name the file "filename.bin" ofc.
How would I have to write it if I get the filename as parameter and save the filename in a string called SavedFilename. And name the file after the string instead of "filename.bin".
Thanks.
You mean like this?
void foo(const char* filename) {
std::ofstream file(filename,
std::ios::out | std::ios::trunc | std::ios::binary);
// ...
}
foo("filename.bin");