【发布时间】:2019-10-15 18:06:49
【问题描述】:
我有一个问题,我想在文本文件中保存所有 ID 号,但它只保存用户输入的最后一个 ID 号。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string line;
int idnum;
ofstream IdData ("data.txt");
cout<<" Enter your ID number: "<<endl;
cin>>idnum;
if(IdData.is_open())
{
IdData<< "ID number of voter is: ";
IdData << idnum << endl;
IdData.close();
}
else
cout<<" Unable to open file";
ifstream Data ("data.txt");
if(Data.is_open())
{
while (getline (Data, line))
{
cout<< line << endl;
}
Data.close();
}
else
cout<<" Unable to open file";
}
【问题讨论】:
-
当你说“最后一个”身份证号码时,你是什么意思?您只需要一个 ID 号!
-
是的,但是在文本文件中我想查看不同用户输入的所有 ID 号
-
顺便说一句,如果
IdData无法打开,您的程序会在打印消息后继续。您可能想在打印消息后使用return EXIT_FAILURE;。
标签: c++ fstream ifstream ofstream