简体   繁体   English

用C ++打开文件

[英]Opening a file in C++

I am trying to open a file stored on my c drive with name test.txt .I am getting a lot of errors.I am new to filing in C++.Please help me out thanks. 我正在尝试打开名称为test.txt c驱动器上存储的文件。我遇到了很多错误。我是C ++归档的新手,请帮我谢谢。 // writing on a text file //在文本文件上写

#include <iostream>
#include <fstream>
using namespace std; 
int main ()
 {
    ofstream mystream;
    mystream.open("C:\\test",ios::in||ios::out);
        /*Check if the file is opened properly*/
    return 0;
}

This 这个

mystream.open("C:\\test",ios::in || ios::out);

should be 应该

mystream.open("C:\\test",ios::in | ios::out);

You are using the logical OR operator ( || ) instead of the bitwise OR operator ( | ). 您使用的是逻辑OR运算符( || ),而不是按位OR运算符( | )。 The former returns a boolean value, while the latter returns the bitwise OR of the two values. 前者返回布尔值,而后者则返回两个值的按位或。

You probably also want to fully qualify the filename. 您可能还希望完全限定文件名。 For example: 例如:

mystream.open("C:\\test.txt", ios::in | ios::out);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM