C++程序不输入文件
C++ program not inputting into a file
这是我目前拥有的:
#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;
似乎对我来说工作不正常?
希望对您有所帮助。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream bookings("Schedule.txt", ofstream::app);
fstream magicians("Magicians.txt");
fstream holidays("Holidays.txt");
string holiday;
while(getline(holidays, holiday)){
bookings << holiday << "\n";
cout << holiday << "\n";
}
bookings.close();
magicians.close();
holidays.close();
return 0;
}
您是否尝试过先声明您的 ifstream 和 ofstream 变量,如下所示?
ifstream inFile;
ofstream 输出文件;
在将内容输入流之前,我使用以下命令打开输入文件。
inFile.open("input.txt");
outFile.open("toutput.txt");
我是 C++ 新手。希望这有帮助。
这是我目前拥有的:
#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;
似乎对我来说工作不正常?
希望对您有所帮助。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream bookings("Schedule.txt", ofstream::app);
fstream magicians("Magicians.txt");
fstream holidays("Holidays.txt");
string holiday;
while(getline(holidays, holiday)){
bookings << holiday << "\n";
cout << holiday << "\n";
}
bookings.close();
magicians.close();
holidays.close();
return 0;
}
您是否尝试过先声明您的 ifstream 和 ofstream 变量,如下所示?
ifstream inFile; ofstream 输出文件;
在将内容输入流之前,我使用以下命令打开输入文件。
inFile.open("input.txt"); outFile.open("toutput.txt");
我是 C++ 新手。希望这有帮助。