-2
fstream myFile("test.txt", ios::in | ios::out | ios::trunc);

How is this line interpreted by the compiler in C++ file handling?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
AzeTeck
  • 15
  • 2
  • 1
    Can you please be more specific about that statement? What part *exactly* are you wondering about? What does your text-books say? What kind of research have you done? Or perhaps you need to invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to begin with? – Some programmer dude Apr 14 '21 at 06:05
  • Illustrate using suitable example, why friend function in needed for operator overloading when you can achieve the same using member function. This is the complete question @Someprogrammerdude – AzeTeck Apr 14 '21 at 06:06
  • Please [edit] your question to include all relevant information. Also please take some time to refresh the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 14 '21 at 06:08
  • Presumably, these names are intended to be `std::fstream`, `std::ios::in`, `std::ios::out`, and `std::ios::trunc`. As written, they have no intrinsic meaning. – Pete Becker Apr 14 '21 at 14:07

2 Answers2

2

ios::out means that you intend to write to the file in contrast to reading ios::in. In your case the stream can be used for both reading and writing.

ios::trunc means that the current file content will be discarded. Compare this to ios::app where you will append to a file if it exists. ios::trunc is implied if not ios::in is specified for example. "When used for an ofstream without ios::app, ios::ate or ios::in, ios::trunc is implied."

Bonus:

iso::binary means that what you will write will be put verbatim into the file (otherwise for windows \n could be replaced with \n\r for example)

Lasersköld
  • 2,028
  • 14
  • 20
0

fstream : This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

Also it using multiple flags as per below:

ios::app : Append mode. All output to that file to be appended to the end.

ios::ate : Open a file for output and move the read/write control to the end of the file.

ios::in : Open a file for reading.

ios::out : Open a file for writing.

ios::trunc : If the file already exists, its contents will be truncated before opening the file.

in your case text.txt file will open for reading, writing and if exists then content will truncated.