【发布时间】:2019-02-11 01:16:30
【问题描述】:
这是我目前拥有的:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream bookings("Schedule.txt");
fstream magicians("Magicians.txt");
fstream holidays("Holidays.txt");
//already tested to make sure these are open & they are
string test;
string holiday;
bookings >> test;
if(test.length()==0){
while(getline(holidays, holiday)){
bookings << holiday;
cout << "test";
}
}
bookings.close();
magicians.close();
holidays.close();
return 0;
}
我的Holidays.txt 包含以下内容:
Veteran's Day
Valentine's Day
Halloween
Christmas
New Years
我已确保我的文件位于正确的目录中并且文件实际上已打开。我逐步完成了程序以确保 holiday 得到相应的线路,但 bookings << holiday; 似乎对我来说不能正常工作?
【问题讨论】:
-
检查文件权限?你应该添加一个
-
尝试同时读取和写入文件可能不是一个好主意。您只需要检查文件是否为空吗?在写入之前尝试关闭文件并重新打开它。
-
@FeiXiang 好像他正在尝试将假期文件附加到预订文件中
-
OP,您使用的是 Windows 吗?也许看看这个:stackoverflow.com/questions/17536570/…
-
我正在使用 xcode。我以前也有
<< endl;,但这没有用。该程序的最终目标是创建一个包含假期的时间表,然后在当天安排任何魔术师,因此我先导入假期然后我会检查他们是否安排在某一天,然后输入魔术师的名字相应地添加到日程文件中。