简体   繁体   English

无法在C ++中打开文件

[英]File not opening in C++

What mistake am I doing that the file is not opening. 我没有打开文件,这是什么错误。 The output screen shows file could not be opened. 输出屏幕显示无法打开文件。 If I separately create ofstream and ifstream constructors than the file is written and read correctly. 如果我分别创建ofstream和ifstream构造函数,则将正确写入和读取该文件。 If I use fstream as below the file is not being created. 如果我如下使用fstream,则不会创建文件。

#include <iostream>
#include <fstream>
using namespace std;
void main(){

    char num[10];
    fstream file;
    file.open("text.txt", ios::ate|ios::in|ios::out| ios::binary);
    if (!file)
    {
        cerr << "File could not be opened" << endl;
        exit(1);
    } // End if

    for (int i = 0; i <= 5; i++){
        cout << "Enter an integer " << endl;
        cin >> num[i];        //Input a number
        file.write((char*)num, sizeof(num));  //Function write to write data to file
    }

    for (int i = 0; i <= 5; i++){
        file.read((char*)num, sizeof(num));  //Function to read data from the file
        cout << endl << num[i] << " ";
    }
    file.close();
    system("pause");
}

You should specify ios::trunc or ios::app depending on whether you want the file to be rewritten or appended to, otherwise it won't be created if it doesn't exist: 您应该根据要重写还是附加文件来指定ios::truncios::app ,否则,如果文件不存在,则将不会创建该文件:

file.open("text.txt", ios::trunc | ios::in | ios::out | ios::binary);

Note that ios::ate doesn't make sense in combination with ios::trunc because the file is truncated. 请注意, ios::ateios::trunc结合使用没有意义,因为该文件被截断了。

You may also take a look at the table of correspondence between ios flags and equivalent stdio mode strings . 您还可以查看ios标志和等效的stdio模式字符串之间的对应关系表 As you can see, the corresponding line of the table for your current code is 如您所见,当前代码表的对应行是

modestring openmode & ~ate  Action if file already exists   Action if file does not exist
"r+b"       binary|out|in        Read from start            Error

PS: And don't forget to change void main() to int main() because the former is undefined behavior. PS:并且不要忘记将void main()更改为int main()因为前者是未定义的行为。

Update: and yes, your whole writing and reading code is wrong. 更新:是的,您的整个读写代码是错误的。 It should be rewritten as follows: 它应该被重写如下:

for (int i = 0; i <= 5; i++){
    cout << "Enter an integer " << endl;
    cin >> num[i];        //Input a number
}
// write the array once and outside of the loop
file.write((char*)num, sizeof(num)); 

// not necessary - just to ensure we read numbers in the next lines
memset(num, 0, sizeof(num)); 
// go to the beginning of the file
file.seekg(0);
// read the array once and outside of the loop
file.read((char*)num, sizeof(num));

for (int i = 0; i <= 5; i++){
    cout << endl << num[i] << " ";
}

DEMO DEMO

You should add 'ios::out' to your file.open 您应该在文件中添加'ios :: out'.open

file.open("text.txt", ios::out);

You can also check if the file was opened easier by using 您还可以通过使用以下命令检查文件是否更容易打开

if (!file.is_open())

EDIT: 编辑:

Probably the combination is not valid. 组合可能无效。 Try to use 尝试使用

file.open("text.txt", ios::app|ios::out|ios::in|ios::binary);

instead 代替

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

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