C++程序未输入到文件中

C++ program not inputting into a file

本文关键字:文件 输入 程序 未输 C++      更新时间:2023-10-16

这是我目前拥有的:

 #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;流出文件;

在将内容输入流之前,我使用以下命令打开输入文件。

inFile.open("input.txt"(;outFile.open("toutput.txt"(;

我是C++新手。希望这有帮助。