| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1174 人关注过本帖
标题:文件读写问题
只看楼主 加入收藏
鹿晗
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-6-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
文件读写问题
要求建立一个关于选课的系统,可是在建立文件的时候发现是乱码的,求帮忙改正~ ~ ~

#include <iostream>
#include <fstream>
using namespace std;

#define Mc_CourseNameLength          61                  /*课程名称不超过30个字*/
#define Mc_FileName                  ("CourseData.csd")   /*存储课程信息的文件名称*/
#define Mc_OpenFileError             0                   /*打开文件失败*/
#define Mc_OpenFileSucceed           1                   /*打开文件成功*/

class ElectiveSystem /*选课系统*/
{
 protected:
  struct Course /*课程*/
  {
    long    ID;                         /*课程编号,0 到 65535*/
    char    name[Mc_CourseNameLength];  /*课程名称*/
    float   credit;                     /*学分*/
    short   TimeTotal;                  /*总学时*/
    short   TimeLesson;                 /*授课学时*/
    short   TimeExperiment;             /*实验学时*/
    short   nature;                     /*课程性质,值域为 1 到 3,每种数值代表一种性质。因为不知道什么是课程的性质,所以没给出具体的含义*/
    bool    term;                       /*为真表示上学期,为假表示下学期*/

    short   number;                     /*节点编号,从 1 开始*/
    struct  Course *next;               /*下一个节点*/
  } *PtHead;                            /*头结点*/

  int       AddNewCourse();                       /*增加新课程信息*/
  int       SaveCourseData(struct Course *cs);    /*保存单个课程信息*/
  int       LoadCourseData();                     /*加载课程信息*/
  void      ShowCourseData();          /*显示课程信息*/
  int       MainInterface();           /*系统主界面*/
  void      InputCourseData();         /*录入课程信息界面*/
  void      SelectCourse();            /*学生选课界面*/
  void      SearchCourse();            /*课程查询界面*/
  void      SearchCredit();            /*按学分查询*/
  void      SearchNature();            /*按性质查询*/
  void      FreePtHeadMemery();        /*释放PtHead指向的空间*/

 public:
  ElectiveSystem();                    /*构造函数*/
  ~ElectiveSystem();                   /*析构函数*/

  void      BeginRun();                /*开始运行此系统*/
};
int ElectiveSystem::AddNewCourse()
{
   struct Course cs;
   cout<<"请输入课程信息,每输入完一个项目按一次Enter键"<<endl<<endl<<endl;
   cout<<"课程编号 (0 到 65535,且必须是唯一的,不能与其他课程编号重复): ";
   cin>>cs.ID;
   cout<<endl;
   cout<<"课程名称 (不超过30个文字): ";
   cin>>cs.name;
   cout<<endl;
   cout<<"课程学分 (0.5 到 5): ";
   cin>>cs.credit;
   cout<<endl;
   cout<<"总学时 (0 到 300): ";
   cin>>cs.TimeTotal;
   cout<<endl;
   cout<<"授课学时: ";
   cin>>cs.TimeLesson;
   cout<<endl;
   cout<<"实验学时: ";
   cin>>cs.TimeExperiment;
   cout<<endl;
   cout<<"课程性质 ( 1 到 3): ";
   cin>>cs.nature;
   cout<<endl;
   cout<<"开课学期 (输入 1 表示上学期,0 表示下学期): ";
   cin>>cs.term;
   cout<<endl;
   return SaveCourseData(&cs);
}
int ElectiveSystem::SaveCourseData(struct Course *cs)
{
   fstream file;
   file.open("Mc_FileName.doc", ios::binary | ios::out | ios::app);
   if (!file)
     return Mc_OpenFileError;
   file.write((char*)&cs->ID, sizeof(long));
   file.write(cs->name, Mc_CourseNameLength * sizeof(char));
   file.write((char*)&cs->credit, sizeof(float));
   file.write((char*)&cs->TimeTotal, sizeof(short));
   file.write((char*)&cs->TimeLesson, sizeof(short));
   file.write((char*)&cs->TimeExperiment, sizeof(short));
   file.write((char*)&cs->nature, sizeof(short));
   file.write((char*)&cs->term, sizeof(bool));
   file.close();
   return Mc_OpenFileSucceed;
}
int ElectiveSystem::LoadCourseData()
{
   ifstream file;
   FreePtHeadMemery();
   PtHead = NULL;
   file.open("Mc_FileName.doc", ios::binary);
   if (!file)
     return Mc_OpenFileError;
   struct Course *cur, *New, test;
   short  num = 1;
   while (!file.eof())
   {
     if (PtHead == NULL)
     {
       test.nature = 0;
       file.read(reinterpret_cast<char*> (&test.ID), sizeof(long));
       file.read(reinterpret_cast<char*> (test.name), Mc_CourseNameLength * sizeof(char));
       file.read(reinterpret_cast<char*> (&test.credit), sizeof(float));
       file.read(reinterpret_cast<char*> (&test.TimeTotal), sizeof(short));
       file.read(reinterpret_cast<char*> (&test.TimeLesson), sizeof(short));
       file.read(reinterpret_cast<char*> (&test.TimeExperiment), sizeof(short));
       file.read(reinterpret_cast<char*> (&test.nature), sizeof(short));
       file.read(reinterpret_cast<char*> (&test.term), sizeof(bool));
       if (test.nature != 0)
       {
         cur = PtHead = new struct Course;
         *cur = test;
         cur->number = num ++;
       }
       else
         break;
     }
     else
     {
       test.nature = 0;
       file.read(reinterpret_cast<char*> (&test.ID), sizeof(long));
       file.read(reinterpret_cast<char*> (test.name), Mc_CourseNameLength * sizeof(char));
       file.read(reinterpret_cast<char*> (&test.credit), sizeof(float));
       file.read(reinterpret_cast<char*> (&test.TimeTotal), sizeof(short));
       file.read(reinterpret_cast<char*> (&test.TimeLesson), sizeof(short));
       file.read(reinterpret_cast<char*> (&test.TimeExperiment), sizeof(short));
       file.read(reinterpret_cast<char*> (&test.nature), sizeof(short));
       file.read(reinterpret_cast<char*> (&test.term), sizeof(bool));
       if (test.nature !=0)
       {
         New = new struct Course;
         *New = test;
         cur->next = New;
         cur = New;
         cur->number = num ++;
       }
       else   /*nature的值域为1 到 3。之前有test.nature  = 0,如果能正确读到数据,那么不应该是0;如果是0,表明到了文件尾部*/
         break; /*因为eof()这个函数到了文件尾部会多停留一次,所以为了解决这个问题,采用此策略*/
     }
   }
   cur->next = PtHead;
   file.close();
   return Mc_OpenFileSucceed;
}
void ElectiveSystem::ShowCourseData()
{
   bool flag = true;
   if (PtHead == NULL)
   {
     LoadCourseData();
     if (PtHead == NULL)
     {
       flag = false;
       cout<<endl<<"管理员尚未录入任何课程信息! 请管理员录入课程信息后,再来浏览!"<<endl;
       system("pause");
     }
   }
   if (flag)
   {
     struct Course *cs = PtHead->next;
     cout<<"编号     学分     总学时   授课学时 实验学时 性质     开课学期 名称"<<endl<<endl;
     cout.precision(2);
     cout.setf(ios::left);
     while (cs->number != 1)
     {
       cout.width(9);
       cout<<cs->ID;
       cout.width(9);
       cout<<cs->credit;
       cout.width(9);
       cout<<cs->TimeTotal;
       cout.width(9);
       cout<<cs->TimeLesson;
       cout.width(9);
       cout<<cs->TimeExperiment;
       cout.width(9);
       cout<<cs->nature;
       if (cs->term == 1)
         cout<<"上学期   ";
       else
         cout<<"下学期   ";
       cout<<cs->name<<endl<<endl;
       cs = cs->next;
     }
     cout.width(9);
     cout<<cs->ID;
     cout.width(9);
     cout<<cs->credit;
     cout.width(9);
     cout<<cs->TimeTotal;
     cout.width(9);
     cout<<cs->TimeLesson;
     cout.width(9);
     cout<<cs->TimeExperiment;
     cout.width(9);
     cout<<cs->nature;
     if (cs->term == 1)
       cout<<"上学期   ";
     else
       cout<<"下学期   ";
     cout<<cs->name<<endl<<endl;
     system("pause");
   }
}
ElectiveSystem::ElectiveSystem()
{
   PtHead = NULL;
   LoadCourseData();
}
ElectiveSystem::~ElectiveSystem()
{
   FreePtHeadMemery();
}
int ElectiveSystem::MainInterface()
{
   short selection = 0;
   cout<<"学生选课系统: 请输入相应的序号,然后按Enter键"<<endl<<endl<<endl;
   cout<<"1.课程录入"<<endl<<endl;
   cout<<"2.浏览课程信息"<<endl<<endl;
   cout<<"3.学生选课"<<endl<<endl;
   cout<<"4.课程查询"<<endl<<endl;
   cout<<"5.退出"<<endl<<endl;
   cout<<"请在此输入你的选择: ";
   cin>>selection;
   return selection;
}
void ElectiveSystem::InputCourseData()
{
   LoadCourseData();
   if (PtHead == NULL)
   {
     short CourseCount = 0;
     int   loop = 0;
     cout<<"你将要录入多少个课程的信息?请输入课程个数,然后按Enter键"<<endl<<endl<<endl;
     cout<<"请在此输入你要录入的课程的个数: ";
     cin>>CourseCount;
     system("cls");
     for ( ; loop < CourseCount; loop ++)
     {
       cout<<"正在录入第 "<<loop + 1<<" 个课程信息,共 "<<CourseCount<<" 个课程信息"<<endl<<endl;
       AddNewCourse();
       system("cls");
     }
     cout<<"录入完成 ! 按任意键返回主界面 ......"<<endl;
     system("pause");
   }
   else
   {
     cout<<"你已经进行过数据录入,要想重新录入数据请将文件Mc_FileName.doc删除!"<<endl<<endl<<endl;
     cout<<"按任意键返回主界面 ......"<<endl<<endl;
     system("pause");
   }
}
void ElectiveSystem::SelectCourse()
{
   LoadCourseData();
   if (PtHead)
   {
     char StuNum[21];
     cout<<"请输入你的学号,以便系统保存你所选择的课程信息。按Enter键结束输入。"<<endl<<endl;
     cout<<"学号 (20个数字以内): ";
     cin>>StuNum;
     system("cls");
     long ID = 0;
     bool flag;
     int  num = 0;
     struct Course *cs;
     struct Course *head = NULL, *cur, *New;
     while (ID != -1)
     {
       if (head)
       {
         struct Course *csT;
         csT = head;
         system("cls");
         cout<<"你的学号是: "<<StuNum<<endl<<endl;
         cout<<"你已经选择了以下课程"<<endl<<endl;
         cout<<"编号     学分     总学时   授课学时 实验学时 性质     开课学期 名称"<<endl<<endl;
         cout.precision(2);
         cout.setf(ios::left);
         while (csT)
         {
           cout.width(9);
           cout<<csT->ID;
           cout.width(9);
           cout<<csT->credit;
           cout.width(9);
           cout<<csT->TimeTotal;
           cout.width(9);
           cout<<csT->TimeLesson;
           cout.width(9);
           cout<<csT->TimeExperiment;
           cout.width(9);
           cout<<csT->nature;
           if (csT->term == 1)
             cout<<"上学期   ";
           else
             cout<<"下学期   ";
           cout<<csT->name<<endl<<endl;
           csT = csT->next;
         }
       }
       cout<<"学生选课: 输入课程的课程编号(输入 -1 结束选课),按Enter键完成输入"<<endl<<endl;
       cout<<"课程编号 (0 到 65535): ";
       cin>>ID;
       flag = 0;
       cs = PtHead->next;
       while (cs->number != 1)
       {
         if (cs->ID == ID)
         {
           if (head == NULL)
           {
             flag = 1; /*成功匹配课程*/
             cur = head = new struct Course;
             *cur = *cs;
             cur->number = ++num;
             cur->next = NULL;
             cout<<"你已成功选择课程 \""<<cs->name<<"\""<<endl<<endl;
             system("pause");
             system("cls");
           }
           else
           {
             struct Course *tcs = head;
             bool flagT = 0; /*标记为没选择过状态*/
             while (tcs)
             {
               if (tcs->ID == cs->ID)
               {
                 flagT = 1; /*该课程已经被选择过*/
                 flag = 1;  /*取消重复的出错提示信息*/
                 cout<<cs->name<<" 已经被你选择过了,不能重复选择! 请重新输入。"<<endl<<endl;
                 system("pause");
                 system("cls");
                 break;
               }
               tcs = tcs->next;
             }
             if (flagT == 0)
             {
               flag = 1; /*成功匹配课程*/
               New = new struct Course;
               *New = *cs;
               New->number = ++num;
               cur->next = New;
               cur = New;
               cur->next = NULL;
               cout<<"你已成功选择课程 \""<<cs->name<<"\""<<endl<<endl;
               system("pause");
               system("cls");
             }
           }
           cs = cs->next;
           break;
         }
         cs = cs->next;
       }
       if (cs->ID == ID)
       {
         if (head == NULL)
         {
           flag = 1; /*成功匹配课程*/
           cur = head = new struct Course;
           *cur = *cs;
           cur->number = ++num;
           cur->next = NULL;
           cout<<"你已成功选择课程 \""<<cs->name<<"\""<<endl<<endl;
           system("pause");
           system("cls");
         }
         else
         {
           struct Course *tcs = head;
           bool flagT = 0; /*标记为没选择过状态*/
           while (tcs)
           {
             if (tcs->ID == cs->ID)
             {
               flagT = 1; /*该课程已经被选择过*/
               flag = 1;  /*取消重复的出错提示信息*/
               cout<<cs->name<<" 已经被你选择过了,不能重复选择! 请重新输入。"<<endl<<endl;
               system("pause");
               system("cls");
               break;
             }
             tcs = tcs->next;
           }
           if (flagT == 0)
           {
             flag = 1; /*成功匹配课程*/
             New = new struct Course;
             *New = *cs;
             New->number = ++num;
             cur->next = New;
             cur = New;
             cur->next = NULL;
             cout<<"你已成功选择课程 \""<<cs->name<<"\""<<endl<<endl;
             system("pause");
             system("cls");
           }
         }
       }
       if (flag == 0 && ID != -1)
       {
         cout<<"课程编号输入有误,没有此课程或有重复选择的课程。"<<endl<<endl;
         system("pause");
         system("cls");
       }
     }
     struct Course *temp;
     /*释放临时空间*/
     while (head)
     {
       temp = head;
       head = head->next;
       delete temp;
     }
   }
   else
   {
     cout<<"管理员尚未录入任何课程信息! 请管理员录入课程信息后,再使用此功能!"<<endl<<endl;
     cout<<"按任意键返回主界面 ......"<<endl<<endl;
     system("pause");
   }
}
void ElectiveSystem::SearchCourse()
{
   LoadCourseData();
   if (PtHead != NULL)
   {
     short selection;
     while (1)
     {
       selection = 0;
       cout<<"课程查询:  请输入相应的序号,然后按Enter键"<<endl<<endl<<endl;
       cout<<"1.按学分查询"<<endl<<endl;
       cout<<"2.按性质查询"<<endl<<endl;
       cout<<"3.返回主界面"<<endl<<endl;
       cout<<"请在此输入你的选择: ";
       cin>>selection;
       switch (selection)
       {
        case 1:
             system("cls");
             SearchCredit();
             system("cls");
        break;
        case 2:
             system("cls");
             SearchNature();
             system("cls");
        break;
        case 3:
             return;
        break;
        default:
         cout<<"输入错误,请重新输入 ......"<<endl<<endl;
         system("pause");
         system("cls");
       }
     }
   }
   else
   {
     cout<<"管理员尚未录入任何课程信息! 请管理员录入课程信息后,再使用此功能!"<<endl<<endl;
     system("pause");
   }
}
void ElectiveSystem::SearchCredit()
{
   float credit;
   short flag = 0;
   struct Course *cs = PtHead->next;
   cout<<"按学分查询"<<endl<<endl;
   cout<<"请输入学分 (0 到 5):  ";
   cin>>credit;
   cout<<endl<<"编号     学分     总学时   授课学时 实验学时 性质     开课学期 名称"<<endl<<endl;
   while (cs->number != 1)
   {
     if (cs->credit == credit)
     {
       flag ++;
       cout.precision(2);
       cout.setf(ios::left);
       cout.width(9);
       cout<<cs->ID;
       cout.width(9);
       cout<<cs->credit;
       cout.width(9);
       cout<<cs->TimeTotal;
       cout.width(9);
       cout<<cs->TimeLesson;
       cout.width(9);
       cout<<cs->TimeExperiment;
       cout.width(9);
       cout<<cs->nature;
       if (cs->term == 1)
         cout<<"上学期   ";
       else
         cout<<"下学期   ";
       cout<<cs->name<<endl<<endl;
     }
     cs = cs->next;
   }
   if (cs->credit == credit)
   {
     flag ++;
     cout.precision(2);
     cout.setf(ios::left);
     cout.width(9);
     cout<<cs->ID;
     cout.width(9);
     cout<<cs->credit;
     cout.width(9);
     cout<<cs->TimeTotal;
     cout.width(9);
     cout<<cs->TimeLesson;
     cout.width(9);
     cout<<cs->TimeExperiment;
     cout.width(9);
     cout<<cs->nature;
     if (cs->term == 1)
       cout<<"上学期   ";
     else
       cout<<"下学期   ";
     cout<<cs->name<<endl<<endl;
   }
   cout<<"共找到 "<<flag<<" 个结果"<<endl<<endl;
   system("pause");
}
void ElectiveSystem::SearchNature()
{
   short nature, flag = 0;
   struct Course *cs = PtHead->next;
   cout<<"按性质查询"<<endl<<endl;
   cout<<"请输入性质 (1 到 3):  ";
   cin>>nature;
   cout<<endl<<"编号     学分     总学时   授课学时 实验学时 性质     开课学期 名称"<<endl<<endl;
   while (cs->number != 1)
   {
     if (cs->nature == nature)
     {
       flag ++;
       cout.precision(2);
       cout.setf(ios::left);
       cout.width(9);
       cout<<cs->ID;
       cout.width(9);
       cout<<cs->credit;
       cout.width(9);
       cout<<cs->TimeTotal;
       cout.width(9);
       cout<<cs->TimeLesson;
       cout.width(9);
       cout<<cs->TimeExperiment;
       cout.width(9);
       cout<<cs->nature;
       if (cs->term == 1)
         cout<<"上学期   ";
       else
         cout<<"下学期   ";
       cout<<cs->name<<endl<<endl;
     }
     cs = cs->next;
   }
   if (cs->nature == nature)
   {
     flag ++;
     cout.precision(2);
     cout.setf(ios::left);
     cout.width(9);
     cout<<cs->ID;
     cout.width(9);
     cout<<cs->credit;
     cout.width(9);
     cout<<cs->TimeTotal;
     cout.width(9);
     cout<<cs->TimeLesson;
     cout.width(9);
     cout<<cs->TimeExperiment;
     cout.width(9);
     cout<<cs->nature;
     if (cs->term == 1)
       cout<<"上学期   ";
     else
       cout<<"下学期   ";
     cout<<cs->name<<endl<<endl;
   }
   cout<<"共找到 "<<flag<<" 个结果"<<endl<<endl;
   system("pause");
}
void ElectiveSystem::FreePtHeadMemery()
{
   if (PtHead)
   {
     struct Course *cs = PtHead->next, *csTemp;
     while (cs->number != 1) /*如果不是首节点,就继续运行;否则退出循环*/
     {
       csTemp = cs;
       cs = cs->next;
       delete csTemp;
     }
     delete cs;
   }
}
void ElectiveSystem::BeginRun()
{
   while(1)
   {
     switch (MainInterface())
     {
      case 1:   /*录入课程信息*/
           system("cls");
           InputCourseData();
           system("cls");
      break;
      case 2:   /*浏览课程信息*/
           system("cls");
           ShowCourseData();
           system("cls");
      break;
      case 3:   /*学生选课*/
           system("cls");
           SelectCourse();
           system("cls");
      break;
      case 4:   /*课程查询*/
           system("cls");
           SearchCourse();
           system("cls");
      break;
      case 5:
           return;    /*退出系统*/
      break;
      default:
       cout<<endl<<"输入有误,请重新输入!"<<endl<<endl;
       system("pause");
       system("cls");
     }
   }
}
void main()
{
   ElectiveSystem es;
   es.BeginRun();
}
搜索更多相关主题的帖子: include 课程 信息 
2015-07-01 16:42
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:4 
too too oooooooooooooo  long long

DO IT YOURSELF !
2015-07-01 16:51
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
收藏
得分:4 
翻半天才翻到头,但还是没找到编译信息,错误描述

一片落叶掉进了回忆的流年。
2015-07-01 17:43
鹿晗
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-6-29
收藏
得分:0 
长也没办法啊  难道文件真的没法正常输出信息吗
2015-07-02 14:49
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:4 
回复 3楼 诸葛欧阳
fstream对象write函数的第二个参数是输入字符串的字符数,要用strlen()函数获取,你看你用成sizeof()了,里面还填的是类型名称。很可能问题就在这里。
2015-07-02 15:36
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
错了,我是想回复楼主
2015-07-02 15:36
鹿晗
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-6-29
收藏
得分:0 
回复 6楼 yangfrancis
求具体怎么改。。。。
2015-07-02 16:46
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
回复 7楼 鹿晗
file.write(cs->name,strlen(cs->name));//写入字串用write
file<<cs->credit;                     //写入数值用<<
2015-07-04 17:22
阿肆
Rank: 1
来 自:江苏 徐州
等 级:新手上路
帖 子:1
专家分:4
注 册:2015-7-5
收藏
得分:4 
这些编码  用什么编的  或是总么运行

虚心求教
2015-07-05 22:17
api163
Rank: 1
等 级:新手上路
帖 子:5
专家分:7
注 册:2015-3-27
收藏
得分:0 
我想问一下这软件开发了为放在哪个企系统用?难道还是黑白框的CMD宣窗口下用。
2015-07-13 19:04
快速回复:文件读写问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033209 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved