C++ ios::in|ios::out 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28338775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is ios::in|ios::out?
提问by Prakhar Verma
I was reading some project code and I found this,here MembersOfLibrary() is a constructor of class MenberOfLibrary
我正在阅读一些项目代码,我发现了这个,这里 MemberOfLibrary() 是类 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
我无法理解 --> ios::in|ios::out <-- 请帮忙!谢谢你
采纳答案by emlai
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 twoios
flags,
meaning that passingios::in | ios::out
to the constructor
ofstd::fstream
enables bothinput and output for the stream.
ios::in
允许来自流的输入(读取操作)。ios::out
允许输出(写操作)到流。|
(位或运算符)被用来在两个结合ios
的标志,
这意味着通过ios::in | ios::out
给构造
的std::fstream
能够既用于流输入和输出。
Important things to note:
需要注意的重要事项:
std::ifstream
automatically has theios::in
flag set.std::ofstream
automatically has theios::out
flag set.std::fstream
has neitherios::in
orios::out
automatically
set. That's why they're explicitly set in your example code.
std::ifstream
自动ios::in
设置了标志。std::ofstream
自动ios::out
设置了标志。std::fstream
没有ios::in
或ios::out
自动
设置。这就是它们在您的示例代码中明确设置的原因。
回答by Arun A S
memberData.open("member.txt", ios::in|ios::out);
ios::in is used when you want to read from a file
ios::in 用于读取文件
ios::out is used when you want to write to a file
ios::out 用于写入文件
ios::in|ios::out means ios::in or ios::out, that is whichever is required is used
ios::in|ios::out 表示 ios::in 或 ios::out,以需要的为准
Here's a useful link
这是一个有用的链接
回答by π?ντα ?ε?
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.
ios::in
和ios::out
是openmode flags,在您的情况下与二进制或( |
) 操作相结合。因此文件被打开用于读取和写入。