打印

C++指针问题

[复制链接]
769|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sinadz|  楼主 | 2012-4-8 21:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面的程序,选择 2 查看的时候,第一次查看可以正常显示信息。第二次就不行了。。。
   
  文件中的内容,大家可以选1 自己输入一些。
#include<fstream>
#include<iostream>
#include<string>
using namespace std;

void main()
{
  fstream file;
  string name,sex,stuno;  
  int ctrl;
  file.open ("sturegin.txt",ios::app|ios::out|ios::in);  
  if(file.fail ())
{
cout<<"文件打开错误!"<<endl;
exit(1);
}
  while(1)
  {  
  cout<<" 1:添加资料 2:查看信息 0:保存退出";
cin>>ctrl;

switch(ctrl)
{
case 1:
while(ctrl==1)
{  
cout<<"请输入姓名: "; cin>>name;
file<<" 姓名:"<<name<<'\n';
cout<<"请输入性别: "; cin>>sex;  
file<<" 性别:"<<sex<<'\n';
cout<<"请输入学号: "; cin>>stuno;
file<<" 学号:"<<stuno<<'\n';
cout<<"1:继续信息录入 0:退出信息录入\n";
cin>>ctrl;
} break;
case 2:
file.seekg(0,ios::beg); // !!!!!!!!
while(!file.eof())
{
getline(file,name,'\n');
getline(file,sex,'\n');
getline(file,stuno,'\n');
cout<<name<<endl;
cout<<sex<<endl;
cout<<stuno<<endl;
}
break;
case 0:
file.close();
return ;
break;

  
}
   
}
}

相关帖子

沙发
无冕之王| | 2012-4-8 21:46 | 只看该作者
在case 1:  
的下面加上
file.seekp(0,ios::end);试试?

使用特权

评论回复
板凳
hsbjb| | 2012-4-9 18:16 | 只看该作者
LZ按LS大侠的方法试试,貌似可行

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

304

主题

2313

帖子

0

粉丝