“无法打开文件。” C ++ fstream

卡斯珀

代码:

int question_3()
{
    fstream hardware("hardware.dat" , ios::binary | ios::in | ios::out);

    if (!hardware)
    {
        cerr << "File could not be opened." << endl;
        exit(1);
    }

    HardwareData myHardwareData;

    for (int counter = 1; counter <= 100; counter++)
    {
        hardware.write(reinterpret_cast< const char * >(&myHardwareData), sizeof(HardwareData));
    }

    cout << "Successfully create 100 blank objects and write them into the file." << endl;
.
.
.

结果:

在此处输入图片说明

为什么无法打开文件?

如果文件“ hardware.dat”不存在,程序将使用该名称创建文件。为什么不?

如果我首先像下面那样创建文件,该程序将继续。

![在此处输入图片描述] [2]


感谢您的关注。


最终解决方案:

int question_3()
{
    cout << "Question 2" << endl;

    fstream hardware;                                         <---Changed
    hardware.open("hardware.dat" , ios::binary | ios::out);   <---Changed

    if (!hardware)
    {
        cerr << "File could not be opened." << endl;
        exit(1);
    }

    HardwareData myHardwareData;

    for (int counter = 1; counter <= 100; counter++)
    {
        hardware.write(reinterpret_cast< const char * >(&myHardwareData), sizeof(HardwareData));
    }

    cout << "Successfully create 100 blank objects and write them into the file." << endl;

    hardware.close();                                                   <---Changed
    hardware.open("hardware.dat" , ios::binary | ios::out | ios::in);   <---Changed
.
.
.

在此处输入图片说明

内曼娅·波瑞克(Nemanja Boric)

为什么要同时使用ios::inios::out标志打开文件(似乎只在写入该文件)?ios::in将需要一个现有文件:

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    fstream f1("test1.out", ios::binary | ios::in | ios::out);
    if(!f1)
    {
        cout << "test1 failed\n";
    }
    else
    {
        cout << "test1 succeded\n";
    }


    fstream f2("test2.out", ios::binary | ios::out);
    if(!f2)
    {
        cout << "test 2 failed\n";
    }
    else
    {
        cout << "test2 succeded\n";
    }
}

输出:

burgos@olivia ~/Desktop/test $ ./a.out 
test1 failed
test2 succeded

也许您想使用ios::app

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法打开fstream C ++文件,即使它与.cpp位置相同

来自分类Dev

fstream 无法在树莓派上写入/打开文件

来自分类Dev

c ++ fstream更改文件中的空间

来自分类Dev

c ++ fstream更改文件中的空间

来自分类Dev

C ++ fstream写入文件非常慢

来自分类Dev

如何在C ++中使用fstream打开子文件夹

来自分类Dev

C ++文件无法打开

来自分类Dev

fstream c ++函数返回整行fstream

来自分类Dev

无法在C ++中使用相同的fstream对象进行读写

来自分类Dev

如何使用fstream :: open()检查C ++中是否存在文件

来自分类Dev

在C ++中使用sstream和fstream写入文件名

来自分类Dev

关于c ++文件I / O和fstream的疑问

来自分类Dev

使用fstream从C ++中的.txt文件获取文本的问题

来自分类Dev

C ++我不能使用fstream变量保存到文件

来自分类Dev

如何使用fstream(C ++)从文件中读取特定行

来自分类Dev

如何在 C++ 中使用 fstream 读取 .txt 文件

来自分类Dev

C ++-fstream和ofstream

来自分类Dev

C ++ cout而不是fstream

来自分类Dev

fstream C ++路径

来自分类Dev

C ++ fstream getline参数

来自分类Dev

C ++ fstream混乱

来自分类Dev

fstream C ++路径

来自分类Dev

使用fstream关闭文件

来自分类Dev

使用fstream读取文件

来自分类Dev

在C中无法打开文件

来自分类Dev

无法打开文件以输入C ++

来自分类Dev

使用FStream无法正常写入文件

来自分类Dev

是否有等效于fstream的boost会导致无法打开文件?

来自分类Dev

为什么[fopen()/ open()/ fstream file]在Mac OS X上无法打开文件?