gpt4 book ai didi

c++ - 写入文件无法正常工作

转载 作者:行者123 更新时间:2023-11-28 02:12:16 24 4
gpt4 key购买 nike

我试图将我的一些输入保存到一个文件中,但它似乎不起作用。我不知道它是文件扩展名还是其他什么,但我已经尝试修改它一个小时了,但是它没有在我的文件夹中弹出一些文件。

这就是我的代码的工作方式(不发布所有内容,会太长)

这是我的功能:

void mobiltelefon::savePhoneOnFile() const
{
ofstream out;
out.open("C:\\temp\\phones.txt", ios::in);
out << this->numberofphones << endl;
for (int i = 0; i < this->numberofphones; i++) {
out << this->phones[i]->getPhonename() << endl;
out << this->phones[i]->getPrice() << endl;
}
out.close();
}

这就是我在 main 中调用它的方式:

case 7:
cout << "Save the phones on file" << endl;
fb.savePhoneOnFile();
break;

我看不出我的错误。当我尝试保存文件时,为什么文件没有显示在我的文件夹中?

最佳答案

如果你试图打开文件进行写入,你应该使用 ios::out 作为第二个参数

ofstream out("C:\\temp\\phones.txt", ios::out);

各种open modes

app seek to the end of stream before each write
binary open in binary mode
in open for reading
out open for writing
trunc discard the contents of the stream when opening
ate seek to the end of stream immediately after open

关于c++ - 写入文件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com