问题
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();
}
I m not able to understand the meaning of --> ios::in|ios::out <-- Please Help out ! Thank You
回答1:
ios::inallows input (read operations) from a stream.ios::outallows output (write operations) to a stream.|(bitwise OR operator) is used to combine the twoiosflags,
meaning that passingios::in | ios::outto the constructor
ofstd::fstreamenables both input and output for the stream.
Important things to note:
std::ifstreamautomatically has theios::inflag set.std::ofstreamautomatically has theios::outflag set.std::fstreamhas neitherios::inorios::outautomatically
set. That's why they're explicitly set in your example code.
回答2:
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.
回答3:
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/
来源:https://stackoverflow.com/questions/28338775/what-is-iosiniosout