2017-03-31 91 views
1

我有一个程序,它使用各种结构和函数从输出文件中读取信息到结构中,对其执行操作,并在符合条件时写入输出文件。一切工作正常,除了应该写入输出文件的功能没有这样做。我需要有一个写入输出文件的函数,所以在main中做不是一个选项。不写入txt文件的函数

编辑:写入输出文件的函数位于最底部。 另一个编辑:如果订阅已过期,我只需要将信息写入输出文件(因此如果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(); 
    } 
} 
+0

您的意思是说'while(customer.custInfo.monthsLeft!= 0){'在该函数中?因为如果调用'expired'时'customer.custName.monthsLeft'不为0,则不会得到任何保存的内容。 – kjpus

+0

如果订阅过期,我只需要它写入文件即可。对不起,我应该提到这一点。 –

+0

它在从输入文件读取的函数内部发生更改。 –

回答

3

在这段代码

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

不过outFile应该是参数openOutFile而不是一个局部变量,否则调用openOutFile(不过outFile );没有公开的流返回outFile

+0

修好了!非常感谢! –