函数不写入 txt 文件

Function not writing to txt file

我有一个程序,它使用各种结构和函数将信息从输出文件读入结构,对其进行处理,并在满足条件时写入输出文件。一切正常,除了应该写入输出文件的函数没有这样做。我需要有一个写入输出文件的函数,所以在 main 中执行它不是一个选项。

编辑:写入输出文件的功能在最底部。 另一个编辑:如果订阅过期,我只需要将信息写入输出文件(所以如果 customer.custInfo.monthsLeft==0)。

这是我的代码:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <climits>

using namespace std;

struct subscriberName{
    string firstName;
    string lastName;
    int custId;
};

struct address{
    string street;
    string city;
    string state;
    int zip_code;
};

struct date{
    string month;
    int day;
    int year;
};

struct renewal_information{
    int monthsLeft;
    date lastNoticeSent;
};

struct subscriber{
    subscriberName custName;
    address custAddress;
    renewal_information custInfo;
};

void openInFile(ifstream&);
void openOutFile(ofstream&);
subscriber recordIn(ifstream&, subscriber&, address&, date&, int&,     int&);
void expired(subscriber&, ofstream&);

int main() {
    ifstream inFile;
    ofstream outFile;
    openInFile(inFile);
    openOutFile(outFile);
    subscriber customer;
    address custAddress;
    date custDate;
    int currentLine=0, numProcessed=0, numExpired=0;

    while (!inFile.eof()){
        recordIn(inFile, customer, custAddress, custDate, currentLine,     numProcessed);

        if (customer.custInfo.monthsLeft==0) {
            expired(customer, outFile);
            numExpired++;
        }
    }
    cout<<endl<<string(47, '-')<<endl<<"Number of subscribers     processed: "<<numProcessed
        <<endl<<"The number of expired subscriptions is: "    <<numExpired<<endl
        <<string(47, '-')<<endl<<endl;

    inFile.close();
    outFile.close();
    return 0;
}

void openInFile(ifstream& inFile){
    string inFileName;
    do{
        cout<<"Enter input file name: ";
        cin>>inFileName;
        cout<<inFileName<<endl;
        inFile.open(inFileName.c_str());
    }
    while (!inFile);
    if (inFile.fail()){
        cout<<"input file failed to open\n";
        inFile.clear();
    } else
        cout<<inFileName<<" opened successfully\n";
}

void openOutFile(ofstream&){
    string outFileName;
    ofstream outFile;
    do{
        cout<<"Enter output file name: ";
        cin>>outFileName;
        cout<<outFileName<<endl;
        outFile.open(outFileName.c_str());
    }
    while (!outFile);
    if (outFile.fail()){
        cout<<"output file failed to open\n";
        outFile.clear();
    } else
        cout<<outFileName<<" opened successfully\n";
}

subscriber recordIn(ifstream& inFile, subscriber& customer, address&     custAddress, date& custDate, int& currentLine, int& numProcessed){
    inFile.ignore(currentLine, '\n');

    getline(inFile, customer.custName.firstName, '\n');

    if (inFile.eof()){
        return customer;
    }
    else {
        getline(inFile, customer.custName.lastName, '\n');
        inFile >> customer.custName.custId;
        cout << "==> Processing Customer ID: " <<     customer.custName.custId << endl;
        numProcessed++;

        inFile.ignore(INT_MAX, '\n');
        getline(inFile, customer.custAddress.street, '\n');
        getline(inFile, customer.custAddress.city, '\n');
        getline(inFile, customer.custAddress.state, '\n');
        inFile >> customer.custAddress.zip_code;
        inFile >> customer.custInfo.monthsLeft;
        inFile >> customer.custInfo.lastNoticeSent.month;
        inFile >> customer.custInfo.lastNoticeSent.day;
        inFile >> customer.custInfo.lastNoticeSent.year;

        currentLine = currentLine + 11;
    }
    return customer;
}

void expired(subscriber& customer, ofstream& outFile){
    while (customer.custInfo.monthsLeft==0) {
        outFile << customer.custName.firstName;
        outFile << customer.custName.lastName;
        outFile << customer.custName.custId;
        outFile << customer.custAddress.street;
        outFile << customer.custAddress.city;
        outFile << customer.custAddress.state;
        outFile << customer.custAddress.zip_code;
        outFile << customer.custInfo.monthsLeft;
        outFile << customer.custInfo.lastNoticeSent.month;
        outFile << customer.custInfo.lastNoticeSent.day;
        outFile << customer.custInfo.lastNoticeSent.year;
        customer.custInfo.monthsLeft=-1;
        outFile.flush();
    }
}

在此代码中

void openOutFile(ofstream&){
    string outFileName;
    ofstream outFile;
    ...
}

outFile 应该是 openOutFile 的参数而不是局部变量,否则调用 openOutFile(outFile ); 不会 return 打开流 outFile