函数不写入txt文件

时间:2017-03-31 18:29:43

标签: c++ function c++11 fstream ostream

我有一个程序,它使用各种结构和函数从输出文件中读取结构中的信息,对它执行某些操作,并在满足条件时写入输出文件。一切正常,除了应该写入输出文件的功能不是这样。我需要有一个写入输出文件的函数,所以在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();
    }
}

1 个答案:

答案 0 :(得分:3)

在此代码中

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

outFile 应该是 openOutFile 的参数,而不是本地变量,否则对 openOutFile(outFile); 的调用不会返回一个开放的流 outFile

相关问题