38

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?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
herzl shemuelian
  • 3,346
  • 8
  • 34
  • 49

3 Answers3

54

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.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 25
    And noting that `trunc` creates an empty file each time. – Bo Persson Jan 12 '12 at 13:54
  • 3
    But what, if I wnat to open for reading and writing and to create the file, if it doesn not exist? Standard libc `open()` can do that by specifying the`O_CREAT` flag. Is that not possible in C++? – Kai Petzke Oct 06 '20 at 09:21
14

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
hyprnick
  • 2,643
  • 2
  • 21
  • 18
  • You can remove that second close(). Simply opening it with the flags "binary | in | out | trunc" will work fine. – TorbenC Feb 06 '15 at 11:05
  • 1
    Very late on this, but, couldn't my_stream.open("my_file_name",std::fstream::binary | std::fstream::trunc | std::fstream::out); simply be my_stream.open("my_file_name", std::ios::out); ? It's just meant to create the file momentarily... – user2962533 Apr 26 '16 at 20:12
0

You can check the reference from std::basic_filebuf::open

If you use out|in which is the default mode, it is equivalent to using r+ as access mode in fopen, so it cannot create a new file.

So if you want to create a fstream that can read and write a new file, you can use in|out|trunc as w+

And you can create or write to end if file exists by using in|app or in|out|app as a+

Ben
  • 141
  • 8