我尝试创建一个程序,打开一个名为" hours1.txt"的文件。它从该文件中读取所有数据,并将其存储到矢量中。然后它应该将所有数据输出到名为" payments1.txt"的文件中。虽然,我甚至无法打开文件我不知道我做错了什么。
using namespace std;
struct PayRecords{
char lastName[50];
char firstName[50];
double hW;
double hoW;
};
int main()
{
vector<PayRecords>payInfo; // creating an empty vector that will hold the data..
PayRecords data; // creating an object that can access each member of the structure..
ifstream infile; // declaring an object that can open the file..
infile.open("hours1.txt"); // opening the file..
ofstream outfile;
outfile.open("payments1.txt");
if(!infile)
{
cerr << "Sorry this file could not be opened! Maybe its not placed in the right folder???" << endl;
return 1;
}
while(infile >> data.lastName >> data.firstName >> data.hW >> data.hoW) // while we arent at the end of the file, keep reading each piece of new data..
{
payInfo.push_back(data); // adding the data to each member of the structure into the vector..
}
for(int i=0; i<payInfo.size(); i++) // this for loop is displaying all the results
{
outfile << "YOUR NAME" << endl;
outfile << "PAY FOR THIS WEEK" << endl;
outfile << "==========================" << endl << endl;
outfile << payInfo[i].lastName << endl;
outfile << payInfo[i].firstName << endl;
outfile << payInfo[i].hW << endl;
outfile << payInfo[i].hoW << endl;
}
cout << payInfo.size();
infile.close();
outfile.close();
cout << "thank you the file has been changed";
}