2

For my project I want to use Fstream to open file and I want to open it for both reading and writing operations at once. I am using Visual Studio.
Normally it would be just file.open(); but since there is a bug in Visual Studio (You can find more on it in this topic How do I operate on file using fstream pointers? in comments to sftrabbit answer) it is not that easy. File.Open() will open the file only for reading (writing will make no effect) and to open in that way file for writing we need to do file.open(path, std::fstream::in | std::fstream::out | std::fstream::trunc) but this will open it only for writing, not for reading.

Any ideas how to open the file with just one line ?

Community
  • 1
  • 1
rank1
  • 1,018
  • 4
  • 16
  • 37
  • "but this will open it only for writing, not for reading" - what makes you think so? – Kiril Kirov Apr 02 '13 at 08:12
  • 1
    the standard specifies `basic_filebuf* open(const char* s, ios_base::openmode mode);` so opening with just open() is not guaranteed to give read/write. Some compilers might have a default value for the mode but it is not a bug if they don't. – msam Apr 02 '13 at 08:26

1 Answers1

3

Simple code.

stream file;
file.open(fileName,ios::in | ios::out);

The symbol to be used between flags is the piping symbol.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
IcyFlame
  • 5,059
  • 21
  • 50
  • 74
  • 1
    Let's call the symbol the `bitwise OR`. Piping is an operation you can do in the Linux OS that is quite different from what this code is doing. – KhanKhuu Feb 23 '21 at 03:05