ZzuYyuRru 2024-06-22 12:10 采纳率: 25%
浏览 5

为什么会显示文件"Student_Information.txt"无法打开!呢

为什么会显示文件"Student_Information.txt"无法打开!呢

img

img


```c++
bool Book::Personnel_System() 
{ 
    while (1) 
    { 
        int menu_options; 
 
        cout << "请输入登录密码:"; 
        cin >> key; 
        if (key == "012345")           //登录密码 
        while (1) 
        { 
            wrongAttempts = 0; // 重置错误次数
            cout << endl; 
            cout << "——————————————" <<endl;
            cout << " 登录密码输入正确!" <<endl; 
            cout << "——————————————" <<endl;
            cout << endl; 
            cout<< "主菜单: " << endl; 
            cout<< "工作人员                 " << endl; 
            cout<< "1——添加图书信息   "<< endl; 
            cout<< "2——修改图书信息" << endl; 
            cout<< "3——删除图书信息   "<< endl; 
            cout<< "4——查询图书信息" << endl; 
            cout<< "5——退出登录"<< endl; 
            cout<< "你需要做什么?(1‐5)" << endl; 
            cin >> menu_options; 
            switch (menu_options) 
            { 
         
                case 1:Book_add(); break; 
                case 2:Book_mod(); break; 
                case 3:Book_del(); break; 
                case 4:Book_search(); break; 
                case 5:return false; 
                default:cout << "输入错误,请重新选择" << endl; break; 
            } 
            if (!(menu_options == 4)) 
            { 
                 cout << "是否确认?        《确认/(Y/y)》    《取消/(N/n)》" << endl; 
                 char yn; 
                 do 
                 { 
                  cin >> yn; 
                 } while (!(yn == 'Y' || yn == 'y' || yn == 'N' || yn == 'n')); 
             
             
                 if (yn == 'Y' || yn == 'y')     { 
             
                     if (menu_options == 1|| menu_options == 2 || menu_options == 3) 
                  { 
                   B_Save_file(); 
                  } 
                  cout << "操作成功"; 
                 } 
    } 
    B_clear(); 
   } 
  else 
  { 
   cout << "密码错误!" << endl; 
   wrongAttempts++;
           if(wrongAttempts >= 5)
           {
            cout << "密码错误次数已达5次,请等待倒计时结束再操作。\n";
            // 启动倒计时线程
            thread countdown(countdownThread);
            countdown.join(); // 等待倒计时线程结束
            }
   continue; 
  } 
 } 
 return true; 
} 
 
//界面初始化函数 
void login_init() 
{ 
 system("cls"); 
 cout << "\n            欢迎进入图书管理系统1.0版" << endl; 
 cout << "             请输入您的登录方式" << endl; 
 cout << "             1 读者管理" << endl; 
 cout << "             2 图书管理" << endl; 
 cout << "             3 借阅管理" << endl; 
 cout << "             4 退出系统" << endl; 
} 
class Student 
{
public:
    int s_sum=0;
  
    string key;             //管理员密码 
    vector<Student> s_array;         //记录学生类 
 
 void S_clear();            //清理已有学生信息 
 void S_Read_file();           //读取学生文件 
 void S_Save_file();           //保存学生文件 
 
 
 bool Student_add();           //添加学生信息 
 bool Student_mod();           //修改学生信息 
 bool Student_del();           //删除学生信息 
 
    bool Student_search(); 
 
 
 bool Student_System();          //学生登录 
 bool Personnel_System();         //工作人员登录 
 void login_init();           //登录界面初始化 
 
    Student() 
 { 
  memset(s_num, 0, sizeof(s_num)); 
  memset(s_name,0, sizeof(s_name)); 
  memset(tell, 0, sizeof(tell, 0, sizeof(tell))); 
  borrow_max = 0; 
  borrow_quantity = 0; 
  memset(borrow_books, 0, sizeof(borrow_books)); 
 
 
 } 
 char s_num[15];      //读者编号 
 char s_name[10];     //姓名 
 char tell[30];     //电话号码 
 int borrow_max;      //最大借阅数量 
 int borrow_quantity;    //借阅数量 
 char borrow_books[10][30];   //借阅图书 
bool S_SetInto();           
friend istream& operator>>(istream& in, Student& cp);  
friend ostream& operator<<(ostream& out, Student& cp);  
bool S_If_match(char p[30]);        
void s_display();           
void Deletenum(); 
}; 
void Student::Deletenum() 
{ 
borrow_quantity -=0; 
} 
//设置学生信息 
bool Student::S_SetInto() 
{ 
    char temp[15]; 
    cout << "请输入学号:(输入+退出)"; 
    cin >> temp; 
    if (temp[0] == '+') 
    { 
        return false; 
    } 
    strcpy(s_num, temp); 
    cout << "学生姓名:"; 
    cin >> s_name; 
    cout << "电话号码:"; 
    cin >> tell; 
    while (borrow_max <= 0 || borrow_max > 10) 
    { 
        cout << "最大借阅数量(1‐10):"; 
        cin >> borrow_max; 
    } 
     
    return true; 
} 
//读取学生文件 
void Student::S_Read_file() 
{ 
ifstream s_file; 
s_file.open("Student_Information.txt"); 
if (!s_file.is_open()) 
{ 
cerr << "文件\"Student_Information.txt\"无法打开\n"; 
exit(1); 
} 
while (!s_file.eof()) 
{ 
Student stu; 
s_file >> stu; 
s_array.push_back(stu); 
s_sum++; 
} 
s_sum--; 
s_file.close(); 
} 
//保存学生文件 
void Student::S_Save_file() 
{ 
ofstream s_file("Student_Information.txt"); 
if (!s_file) 
{ 
} 
cerr << "文件\"Student_Information.txt\"无法打开!\n"; 
exit(1); 
int i = -1; 
while (++i < s_sum) 
{ 
s_file << s_array[i]; 
} 
s_file.close(); 
} 
//添加学生信息 
bool Student::Student_add() 
{ 
    S_Read_file(); 
    Student _stu; 
     cout << "请进行信息录入。按“+”结束!\n"; 
     do 
     { 
          s_array.push_back(_stu); 
     } 
     while (s_array[s_sum++].S_SetInto()); 
     s_sum--; 
     s_array.pop_back(); 
     S_Save_file();
     return true; 
} 
bool Student::Personnel_System() 
{ 
    while (1) 
    { 
        int menu_options; 
 
        cout << "请输入登录密码:"; 
        cin >> key; 
    if (key == "012345")           //登录密码 
       while (1) 
       { 
           wrongAttempts = 0; // 重置错误次数
        cout << endl; 
        cout << "——————————————" <<endl;
        cout << " 登录密码输入正确!" <<endl; 
        cout << "——————————————" <<endl;
        cout << endl;
        cout<< " 主菜单:" << endl; 
        cout<< "工作人员 " << endl; 
        cout<< "1——添加学生信息 "<< endl; 
        cout<< "2——修改学生信息" << endl; 
        cout<< "3——删除学生信息 "<< endl; 
        cout<< "4——查询学生信息" << endl; 
        cout<< "5——退出登录"<< endl; 
        cout<< "你需要做什么?(1-9)" << endl; 
        cin >> menu_options; 
        switch (menu_options) 
        { 
            case 1:Student_add(); break; 
            case 2:Student_mod(); break; 
            case 3:Student_del(); break; 
            case 4:Student_search(); break; 
            case 5:return false; 
            default:cout << "输入错误,请重新选择" << endl; break; 
        } 
        
        if (!(menu_options == 4)) 
        { 
            cout << "是否确认?        《确认/(Y/y)》    《取消/(N/n)》" << endl; 
            char yn; 
            do 
             { 
                  cin >> yn; 
             } while (!(yn == 'Y' || yn == 'y' || yn == 'N' || yn == 'n')); 
            if (yn == 'Y' || yn == 'y') 
            { 
                  if (menu_options == 1 || menu_options == 2 || menu_options == 3) 
                  { 
                       S_Save_file(); 
                  } 
 
                  cout << "操作成功"; 
             } 
        } 
        S_clear(); 
 
       } 
      else 
      { 
           cout << "密码错误!" << endl; 
           wrongAttempts++;
           if(wrongAttempts >= 5)
           {
            cout << "密码错误次数已达5次,请等待倒计时结束再操作。\n";
            // 启动倒计时线程
            thread countdown(countdownThread);
            countdown.join(); // 等待倒计时线程结束
            }
           continue; 
      } 
 } 
 return true; 
} 
int main() 
{ 
    Book xiangnan; 
    Student duzhe; 
    management mag; 
 //若文件不存在,则新建文件 
 //存放学生信息 
 ofstream Student_Information("Student_Information.txt", ios::app); 
 if (!Student_Information) 
 { 
  cerr << "文件\"flight information.dat\"无法打开!\n"; 
  exit(1); 
 } 
 Student_Information.close(); 
 
 //存放图书信息 
 ofstream Book_Information("Book_Information.txt", ios::app); 
 if (!Book_Information) 
 { 
  cerr << "文件\"flight information.dat\"无法打开!\n"; 
  exit(1); 
 } 
 Book_Information.close(); 
 
    ofstream S_Information("S_Information.txt", ios::app); 
 if (!S_Information) 
 { 
  cerr << "文件\"flight information.dat\"无法打开!\n"; 
  exit(1); 
 } 
 S_Information.close(); 
 
 int dlry;          //登陆人员 
 while (1) 
 { 
  login_init();       //界面初始化 
  cin >> dlry; 
  if (dlry == 1) 
  { 
   duzhe.Personnel_System(); 
  } 
  else if (dlry == 2) 
  { 
   xiangnan.Personnel_System(); 
  } 
  else if (dlry == 3) 
  { 
      mag.Student_System(); 
  } 
  else if (dlry == 4) 
  { 
   return 0; 
  } 
  else 
  { 
   cout << "输入错误,请重新选择!" << endl; 
  } 
 } 
return 0; 
}


  • 写回答

1条回答 默认 最新

  • 阿里嘎多学长 2024-06-22 12:23
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据你提供的代码和截图信息,显示“文件"Student_Information.txt"无法打开!”的错误信息可能由以下几个原因造成:

    1. 文件路径问题:如果文件不存在于程序尝试访问的目录中,或者路径指定错误,程序将无法打开文件。请检查文件是否位于程序的当前工作目录下。

    2. 文件权限问题:程序可能没有足够的权限去读取或写入指定的文件。请确保程序有访问该文件的权限。

    3. 文件打开模式问题:在代码中,你使用了ofstream来打开文件,使用了ios::app模式,这意味着如果文件不存在,ofstream不会自动创建文件。你需要检查是否使用了正确的打开模式,或者在打开文件前检查文件是否存在。

    4. 错误处理逻辑:在S_Save_file函数中,当无法打开文件时,你使用了cerr来输出错误信息,但并没有返回错误或者做进一步的错误处理。这可能导致程序在遇到错误时继续执行,而不是停止或抛出异常。

    5. 文件存在性问题:根据第二个截图,Student Information文件在2024/6/22 12:03时大小为0KB,这表明文件可能被创建了但内容为空或者文件被删除了。确保文件在程序尝试访问时存在且有内容。

    6. 程序逻辑问题:在Student::Student_add函数中,你调用了S_Read_file函数,但之后又立即进行了学生信息的添加操作,这可能导致读取的文件信息被新添加的信息覆盖。确保逻辑正确,读取文件后的处理是你所期望的。

    为了解决这个问题,你可以尝试以下步骤:

    • 确保"Student_Information.txt"文件在程序的预期目录中,并且文件名和路径完全正确。
    • 检查程序是否有权限访问该文件。如果没有,尝试以管理员身份运行程序或修改文件权限。
    • 检查文件是否为空或不存在,如果是,确保程序逻辑能够处理这种情况。
    • 审查和改进错误处理逻辑,确保在无法打开文件时能够给出清晰的提示,并采取适当的错误处理措施。
    • 如果文件确实存在但程序仍然报告无法打开,尝试在程序中添加更多的日志输出,以帮助定位问题。

    如果你需要进一步的帮助,可以提供更多的上下文信息,例如程序的完整执行环境和操作系统信息。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月22日