猿博士 2021-04-24 14:48 采纳率: 100%
浏览 158
已采纳

使用c++文件读取ifstream出现了问题,求指点(代码写的有点lj,勿喷)

主要看类Reader,R_b和main函数

 

#include <iostream>
#include <windows.h>
#include <conio.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<iomanip>
#include <string>
#define OK true
#define Error false
#define MAXSIZE 10
typedef bool Status;
typedef int ElemType;
using namespace std;
class R_b;
class Reader;
class Book;
bool flag1=false,flag2=false,flag3=false;
void writetostudenttxt(Reader &r);
void displaystudenttxt(Reader r);
void SetPostion(unsigned short x,unsigned short y)
{
    HANDLE h;
    h=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(h,pos);
}
void LoginMenu();//登录界面实现
int UserShowMenu()//用户菜单
{
    int choice;
    do
    {
        system("cls");
        printf("-----欢迎来到图书管理系统--用户区---\n");
        printf("     1.图书借阅\n");
        printf("     2.图书续借\n");
        printf("     3.图书归还\n");
        printf("     4.用户图书借阅记录\n");
        printf("     5.返回登录界面\n");
        printf("     6.退出\n");
        cin>>choice;
    }
    while(choice<1||choice>5);
    return choice;
}
int AdminShowMenu()//管理员菜单
{
    int choice;
    do
    {
        system("cls");
        printf("-----欢迎来到图书管理系统--管理员区---\n");
        printf("     1.添加图书\n");
        printf("     2.图书浏览\n");
        printf("     3.修改图书\n");
        printf("     4.删除图书\n");
        printf("     5.添加用户\n");
        printf("     6.删除用户\n");
        printf("     7.查看用户\n");
        printf("     8.返回登录界面\n");
        printf("     9.退出\n");
        cin>>choice;
    }
    while(choice<1||choice>10);
    return choice;
}
class Reader//读者类
{
public:
    char *r_name;
    char *r_id;
    char *brrow;//借书记录
    Reader()
    {
        r_name=new char[20];
        r_id=new char[20];
        brrow=new char[99];
    }
    Reader(char *name,char *id,char *b)
    {
        int len=sizeof(name);
        r_name=new char[len+1];
        len=sizeof(id);
        r_id=new char[len+1];
        len=sizeof(b);
        brrow=new char[len+1];
        strcpy(r_name,name);
        strcpy(r_id,id);
        strcpy(brrow,b);
    }
    ~Reader()
    {
        delete []r_name;
        delete []r_id;
        delete []brrow;
    }
    Reader &operator=(Reader &r)
    {
        if(this==&r)
            return *this;
        int len;
        len=sizeof(r.GetName());
        r_name=new char[len+1];
        len=sizeof(r.Getid());
        r_id=new char[len+1];
        len=sizeof(r.GetBrrow());
        brrow=new char[len+1];
        strcpy(r_name,r.r_name);
        strcpy(r_id,r.r_id);
        strcpy(brrow,r.brrow);
        return *this;
    }
    void input()
    {
        cin>>r_name>>r_id>>brrow;
    }
    char* Getid()
    {
        return r_id;
    }
    char* GetName()
    {
        return r_name;
    }
    char *GetBrrow()
    {
        return brrow;
    }
    void BorrowRecord()//借书记录
    {
        gets(brrow);
    }
    bool Match(char*str,short flag)//fLag=1,与名字进行匹配  flag=2,与学号进行匹配
    {
        return flag==1?strcmp(r_name,str)==0:str==r_id;
    }
    void Display()
    {
        cout<<r_name<<"  "<<r_id<<"  "<<brrow<<endl;
    }
};
class Book//图书类
{
private:
    char b_name[20];//书名
    char b_id[20];//书号
    char auther[20];//作者
public:
    Book() {}
    char *Getid()
    {
        return b_id;
    }
    char *GetName()
    {
        return b_name;
    }
    Book(char *name,char* id,char *a)
    {
        strcpy(name,b_name);
        strcpy(id,b_id);
        strcpy(a,auther);
    }
    bool Match(char*str,short flag)//fLag=1,与名字进行匹配  flag=2,与学号进行匹配   flag=3,与作者名字匹配
    {
        return flag==1?(flag==2?strcmp(b_name,str)==0:strcmp(auther,str)):strcmp(b_id,str)==0;
    }
    void Display()
    {
        cout<<setw(12)<<setiosflags(ios::left)<<b_name<<setw(12)<<b_id<<setw(12)<<auther<<setw(20)<<endl;
    }
};
class R_b//执行操作
{
private:
    Reader reader[MAXSIZE];
    Book book[MAXSIZE];
    int r_size;//读者数量
    int b_size;//书籍数量
public:
    R_b()
    {
        int i,j;
        ifstream is;
        is.open ("STUDENT.txt", ios_base::in |ios_base::binary );
        if(!is) cout<<"文件未打开!"<<endl;
        is.seekg (0, ios_base::end);
        r_size = is.tellg()/sizeof(Reader);
        is.seekg (0, ios_base::beg);
        for( i=0; i<r_size; i++)
        {
            is.read((char*)&reader[i],sizeof(reader[i]));
            //cout<<reader[i].r_name<<"  "<<reader[i].r_id<<"  "<<reader[i].brrow<<endl;
        }
        b_size=0;
        is.close();
    }
    ~R_b() {}
    int Getr_size()
    {
        return r_size;
    }
    bool Pushreader();//添加读者
    int Searchreader();//查看读者情况
    bool Deletereader();//删除用户
    bool Pushbook();//添加图书
    int Searchbook();//查看图书
    bool Deletebook();//删除图书
    bool Borrowbook();//借阅图书
    void r_Display()//用户显示区
    {
        for(int i=0; i<r_size; i++)
            reader[i].Display();
        system("pause");
    }
    void b_Display()//图书展示区
    {
        cout<<endl<<setw(12)<<setiosflags(ios::left)<<"书号"<<setw(12)<<"书名"<<setw(12)<<"作者"<<endl;
        for(int i=0; i<b_size; i++)
            book[i].Display();
        cout<<setiosflags(ios::left);
        system("pause");
    }
};
void writetostudenttxt(Reader &r)
{
    ofstream ofs;
    ofs.open("STUDENT.txt", ios_base::app| ios_base::binary);
    if(!ofs)
    {
        cout<<"打开文件失败!"<<endl;
        return ;
    }
    ofs.write((char*)&r,sizeof(r));
    ofs.close();
}
void displaystudenttxt(Reader r)
{
    ifstream ifs("STUDENT.txt",ios_base::in | ios_base::binary);
    while(ifs.read((char*)&r,sizeof(r)))
        r.Display();
    ifs.close();
}
int main()
{
    //LoginMenu();
    R_b tmp;
    if(tmp.Pushreader()) cout<<"添加成功!"<<endl;
    cout<<tmp.Getr_size()<<endl;//输出STUDENT.txt文件中reader的数量
    tmp.r_Display();
    return 0;
}
bool R_b::Pushreader()
{
    char n[20],i[20],t[99];
    cout<<"请输入姓名:";
    cin>>n;
    int idx;
    cout<<"输入学号:";
    cin>>i;
    for(idx=0; idx<r_size; idx++)
    {
        if(reader[idx].Match(i,2))
        {
            cout<<"学号重复"<<endl;
            system("pause");
            return false;
        }
    }
    cout<<"请说明该同学的借阅情况: ";
    cin>>t;
    Reader tmp(n,i,t);
    cout<<"***"<<endl;
    reader[r_size++]=tmp;
    writetostudenttxt(tmp);
    return true;
}
int R_b::Searchreader()
{
    int choice;
    do
    {
        cout<<"请问你是按照什么条件搜索?1.姓名 2.学号";
        cin>>choice;
    }
    while(choice!=1&&choice!=2);
    char t[20];
    cout<<(choice==1?"姓名":"学号")<<":";
    cin>>t;
    int result=0;
    char nod;
    while(1)
    {
        for(int i=0; i<r_size; i++)
        {
            if(reader[i].Match(t,choice))
            {
                reader[i].Display();
                return i;
            }
            return -1;
        }
        if(result==-1)
        {
            cout<<"没有找到信息";
            system("pause");
            return result;
        }
        cout<<"这是要找的人吗?(y/n)";
        cin>>nod;
        if(nod=='y'||nod=='Y')return result;
        else result++;
    }
}
bool R_b::Deletereader()
{
    char choice;
    short result;
    result=Searchreader();
    if(result==-1) return false;
    cout<<"是否删除这条信息(y/n)";
    cin>>choice;
    short idx;
    if(choice=='Y'||choice=='y')
    {
        for(idx=result; idx<r_size; idx++)
            reader[idx]=reader[idx+1];
        r_size--;
        cout<<"删除成功";
        return false;
    }
    cout<<"信息未删除";
    return true;
}
bool R_b::Pushbook()
{
    char n[20],i[20],a[20];
    cout<<"请输入书名:";
    cin>>n;
    cout<<"请输入作者:";
    cin>>a;
    int idx;
    do
    {
        cout<<"输入书号:";
        cin>>i;
        for(idx=0; idx<b_size; idx++)
            if(book[idx].Match(i,2))
            {
                cout<<"书号重复";
                system("pause");
                break;
            }
    }
    while(idx<b_size);
    Book tmp(n,i,a);
    book[b_size++]=tmp;
    return true;
}
int R_b::Searchbook()
{
    int choice;
    do
    {
        cout<<"请问你是按照什么条件搜索?1.书名 2.书号 3.作者";
        cin>>choice;
    }
    while(choice!=1&&choice!=2&&choice!=3);
    char t[20];
    cout<<(choice==1?"姓名":(choice==2?"学号":"作者"))<<":";
    cin>>t;
    int result=0;
    char nod;
    while(1)
    {
        for(int i=0; i<r_size; i++)
        {
            if(book[i].Match(t,choice))
            {
                book[i].Display();
                return i;
            }
            return -1;
        }
        if(result==-1)
        {
            cout<<"没有找到信息";
            system("pause");
            return result;
        }
        cout<<"这是要找的书吗?(y/n)";
        cin>>nod;
        if(nod=='y'||nod=='Y')return result;
        else result++;
    }
}
bool R_b::Deletebook()
{
    char choice;
    short result;
    result=Searchbook();
    if(result==-1) return false;
    cout<<"是否删除这本书(y/n)";
    cin>>choice;
    short idx;
    if(choice=='Y'||choice=='y')
    {
        for(idx=result; idx<b_size; idx++)
            book[idx]=book[idx+1];
        b_size--;
        cout<<"删除成功";
        system("pause");
        return false;
    }
    cout<<"信息未删除";
    return true;
}

//***************
bool R_b::Borrowbook()//借书
{
    char i[20];
    char i_[20];
    bool f1=false,f2=false;
    int idx,idx_;
    cout<<"请输入借阅者的学号:   ";
    cin>>i;
    for(idx=0; idx<r_size; idx++)
    {
        if(i==reader[idx].Getid())
        {
            cout<<reader[idx].GetName()<<"同学存在!";
            system("pause");
            f1=true;
            break;
        }
    }
    if(!f1)
        return false;

    //******
    do
    {
        cout<<"输入书号:";
        cin>>i_;
        for(idx_=0; idx_<b_size; idx_++)
            if(i_==book[idx_].Getid())
            {
                cout<<"《"<<book[idx].GetName()<<"》"<<"存在!";
                system("pause");
                return false;
            }
    }
    while(idx_<b_size);
    char c;
    cout<<i<<"同学,确认借此书吗?(Y/N)"<<endl;
    cin>>c;
    if(c!='Y'||c!='y')
        return false;
    else
    {
        reader[idx_].BorrowRecord();
    }
    return true;
}
void LoginMenu()
{
    string user="ggq",test1,test2,passwd="123";
    string AmPasswd("asd123"),test3;
    int choice=0;
    char c=0;
    HANDLE h;
    h=GetStdHandle(STD_OUTPUT_HANDLE);

    while(1)
    {
        system("cls");
        SetPostion(35,10);
        SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
        cout<<"图书管理系统"<<endl;
        SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
        if(c==80)//下键
            choice=(choice==4?1:choice+1);
        if(c==72)//上键
            choice=(choice==1?4:choice-1);
        if(c==VK_RETURN && choice==3)//在登录选项退出
        {
            if(test1.empty())
            {
                cout<<"用户名为空!";
                system("pause");
                choice=1;
            }
            else if(test2.empty())
            {
                cout<<"密码为空!";
                system("pause");
                choice=1;
            }
            else
                break;
        }
        if(c==VK_RETURN && choice==4)//在管理员登录选项退出
        {
            if(test3.empty())
                cout<<"密码为空!"<<endl;
            else if(test3==AmPasswd)
            {
                cout<<"密码正确!\n正在进入系统,请稍后...";
                Sleep(3000);
                return ;
            }
            else
            {
                cout<<"用户名或密码错误,请重新输入!"<<endl;
                system("pause");
                flag3=false;
                system("cls");
                LoginMenu();
            }
        }
        SetPostion(25,12);
        if(choice==1)
            SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_GREEN);
        cout<<"Username:"<<endl;
        SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
        SetPostion(25,14);
        if(choice==2)
            SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_GREEN);
        cout<<"Password:"<<endl;
        SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
        SetPostion(25,16);
        if(choice==3)
            SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_GREEN);
        cout<<"Login"<<endl;
        SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
        SetPostion(25,18);
        if(choice==4)
            SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_GREEN);
        cout<<"Admin Login"<<endl;
        SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);


        if(c==VK_RETURN && choice==1 && !flag1)
        {
            cin>>test1;
            cout<<endl;
            flag1=true;
        }
        if(c==VK_RETURN && choice==2 && !flag2)
        {
            cin>>test2;
            cout<<endl;
            flag2=true;
        }
        if(c==VK_RETURN && choice==4 && !flag3)
        {
            cout<<"请输入管理员密码:"<<endl;
            //system("cls");
            cin>>test3;
            flag3=true;
        }
        if(!c) c=getch();
        c=getch();
    }//while循环体
    if(user!=test1 ||passwd!=test2 )
    {
        cout<<"用户名或密码错误,请重新输入!"<<endl;
        system("pause");
        flag1=false;
        flag2=false;
        system("cls");
        LoginMenu();
    }
    if(user==test1 ||passwd==test2)
    {
        cout<<"登录成功!\n正在进入系统,请稍后...";
        Sleep(3000);
        return ;
    }
}

困惑我好几天了,求指导

 

 

 

  • 写回答

5条回答 默认 最新

  • 关注

    参考:

    c++中一次读取整个文件的内容的方法:

    1. 读取至char*的情况
    std::ifstream t;  
    int length;  
    t.open("file.txt");      // open input file  
    t.seekg(0, std::ios::end);    // go to the end  
    length = t.tellg();           // report location (this is the length)  
    t.seekg(0, std::ios::beg);    // go back to the beginning  
    buffer = new char[length];    // allocate memory for a buffer of appropriate dimension  
    t.read(buffer, length);       // read the whole file into the buffer  
    t.close();                    // close file handle  
      
    // ... do stuff with buffer here ...  
    
    1. 读取至std::string的情况:

    第一种方法:

    #include <string>  
    #include <fstream>  
    #include <streambuf>  
      
    std::ifstream t("file.txt");  
    std::string str((std::istreambuf_iterator<char>(t)),  
                     std::istreambuf_iterator<char>()); 
    

    第二种方法:

    #include <string>  
    #include <fstream>  
    #include <sstream>  
    std::ifstream t("file.txt");  
    std::stringstream buffer;  
    buffer << t.rdbuf();  
    std::string contents(buffer.str());
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!