马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 跑江湖 于 2013-12-13 14:58 编辑 #include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<windows.h>
using namespace std;
struct Fishc
{
string name;
char sex;
string Id;
};
int main(void)
{
ofstream outFile;
ifstream inFile;
int i;
int n = 0;
int count = 0;
cout << "1. 打印输入到屏幕\n";
cout << "2. 录入数据\n";
cout << "3. 退出程序\n";
cout << "请选择:";
cin >> i;
cin.get();
switch (i)
{
case 1:
count = 1;
break;
case 2:
count = 2;
break;
case 3:
count = 3;
break;
}
if (count == 1)
{
inFile.open("test.txt",ios_base::binary);
if (!inFile.is_open())
{
cout<<"文件无法打开.\n";
exit(EXIT_FAILURE);
}
while (1)
{
if (inFile.eof())
{
break;
}
else
{
Fishc p;
inFile >> p.Id;
inFile >> p.name;
inFile >> p.sex;
cout << p.Id << endl;
cout << p.name << endl;
cout << p.sex<<'\n';
}
}
inFile.close();
}
else if(count == 2)
{
Fishc p;
cout << "请输入姓名:";
getline(cin,p.name);
cin.get(); // 处理回车
cout<<"请输入性别【M(男)/F(女)】:";
cin >> p.sex;
cin.get(); // 处理回车
cout<<"请输入编号 :";
getline(cin,p.Id);
outFile.open("test.txt",ios_base::in|ios_base::app|ios_base::binary);
if (!outFile.is_open())
{
cout<<"打开失败\n";
exit(EXIT_FAILURE);
}
outFile << endl<<'\n';
outFile << p.Id << endl;
outFile << p.name << endl;
outFile << p.sex << endl;
outFile.close();
}
else
{
cout<<"输入任意字符退出程序: ";
cin.get();
}
return 0;
为什么后面多输出了一个F?
|