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();
}
}
您的意思是說'while(customer.custInfo.monthsLeft!= 0){'在該函數中?因爲如果調用'expired'時'customer.custName.monthsLeft'不爲0,則不會得到任何保存的內容。 – kjpus
如果訂閱過期,我只需要它寫入文件即可。對不起,我應該提到這一點。 –
它在從輸入文件讀取的函數內部發生更改。 –