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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:57:17  来源:igfitidea点击:

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

c++file-handling

提问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::inallows input (read operations) from a stream.
  • ios::outallows output (write operations) to a stream.
  • |(bitwise OR operator) is used to combine the two iosflags,
    meaning that passing ios::in | ios::outto the constructor
    of std::fstreamenables bothinput and output for the stream.
  • ios::in允许来自流的输入(读取操作)。
  • ios::out允许输出(写操作)到流。
  • |(位或运算符)被用来在两个结合ios的标志,
    这意味着通过ios::in | ios::out给构造
    std::fstream能够用于流输入和输出。


Important things to note:

需要注意的重要事项:

  • std::ifstreamautomatically has the ios::inflag set.
  • std::ofstreamautomatically has the ios::outflag set.
  • std::fstreamhas neither ios::inor ios::outautomatically
    set. That's why they're explicitly set in your example code.
  • std::ifstream自动ios::in设置了标志。
  • std::ofstream自动ios::out设置了标志。
  • std::fstream没有ios::inios::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

这是一个有用的链接

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

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

回答by π?ντα ?ε?

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

ios::inios::outopenmode flags,在您的情况下与二进制( |) 操作相结合。因此文件被打开用于读取和写入。