该程序从计算机上的某个位置检索文件并将其打印到仅显示员工 SSN 的最后四位数字的屏幕上。
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>
using namespace std;
int main()
{
double Thanks_for_your_time;
string filename = "C:\\Emp\\employee_info.txt";
string line;
ifstream inFile;
inFile.open("C:\\Emp\\employee_info.txt"); // open the file with the
// external name
if (inFile.fail()) // check for a successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
cout << "\nThe file has been successfully opened for reading\n"
<< endl;
while (getline(inFile,line))
cout << line << endl;
// statements to read data from the file would be placed here
do
{
cout << "\nThanks for your time(0 to quit):";
cin >> Thanks_for_your_time;
}
/*
文件已成功打开读取
员工姓名:Harry Heck 员工 SSN:987-98-7987(除最后四个之外的所有内容都需要为“x”或空白) 员工时薪:20.15 美元 本周工作小时数:40.25 总工资:811.04 美元
员工姓名:Sally Smothers 员工 SSN:654-65-4654(除最后四个之外的所有内容都需要为“x”或空白) 员工时薪:50.25 美元 本周工作小时数:40.35 总工资:2027.59 美元
感谢您的宝贵时间(0 退出):*/