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

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?

1

3 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.
4
 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

ios::in and ios::out are openmode flags, and in your case combined with a binary or (|) operation. Thus the file is opened for reading and writing.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like