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
?
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.
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.
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.
ios::app. Opens an output file for appending only. ios::ate. Opens an existing file (either input or output) and seeks to the end.
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,ios::in | ios::out
to the constructorstd::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 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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With