Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ios::in|ios::out?

Tags:

I was reading some project code and I found this,here MembersOfLibrary() is a constructor of class MenberOfLibrary

class MembersOfLibrary {
  public:
    MembersOfLibrary();
    ~MembersOfLibrary() {}
    void addMember();
    void removeMember();
    unsigned int searchMember(unsigned int MembershipNo);
    void searchMember(unsigned char * name);
    void displayMember();

  private:
    Members    libMembers;

};

MembersOfLibrary::MembersOfLibrary() {

    fstream memberData;
    memberData.open("member.txt", ios::in|ios::out);
    if(!memberData) {
    cout<<"\nNot able to create a file. MAJOR OS ERROR!! \n";
    }
    memberData.close();
}

What is ios::in|ios::out?

like image 380
Prakhar Verma Avatar asked Feb 05 '15 07:02

Prakhar Verma


People also ask

What does ios :: app mean in C++?

ios::ate "sets the stream's position indicator to the end of the stream on opening." ios::app "set the stream's position indicator to the end of the stream before each output operation." This means the difference is that ios::ate puts your position to the end of the file when you open it.

What is ios :: binary in C++?

ios::binary makes sure the data is read or written without translating new line characters to and from \r\n on the fly. In other words, exactly what you give the stream is exactly what's written.

What happens when we open a file in ios :: trunc mode and ios :: app mode?

ios::app (short for append) means that instead of overwriting the file from the beginning, all output operations are done at the end of the file. This is only meaningful if the file is also open for output. ios::trunc (short for truncate) means that when the file is opened, the old contents are immediately removed.

What is use of ios :: app mode in file opening?

ios::app. Opens an output file for appending only. ios::ate. Opens an existing file (either input or output) and seeks to the end.


2 Answers

  • ios::in allows input (read operations) from a stream.
  • ios::out allows output (write operations) to a stream.
  • | (bitwise OR operator) is used to combine the two ios flags,
    meaning that passing ios::in | ios::out to the constructor
    of std::fstream enables both input and output for the stream.

Important things to note:

  • std::ifstream automatically has the ios::in flag set.
  • std::ofstream automatically has the ios::out flag set.
  • std::fstream has neither ios::in or ios::out automatically
    set. That's why they're explicitly set in your example code.
like image 53
emlai Avatar answered Oct 09 '22 20:10

emlai


 memberData.open("member.txt", ios::in|ios::out);

ios::in is used when you want to read from a file

ios::out is used when you want to write to a file

ios::in|ios::out means ios::in or ios::out, that is whichever is required is used

Here's a useful link

http://www.cplusplus.com/doc/tutorial/files/

like image 26
Arun A S Avatar answered Oct 09 '22 21:10

Arun A S