使用fstream写入文件

时间:2015-05-26 21:07:14

标签: c++ fstream

我尝试使用以下代码写入fstream的文件。它接受第一个条目,当我点击 Enter 时,它进入菜单的无限循环,而不是允许我输入我的第二个和第三个条目。

我该如何解决这个问题?

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;


struct Course { // structure definition

char cID[3]; 
char title[40];
int creaditHour;
};


void showChoices();

void writeCourse(fstream& file, char fileName[]);



 int main ()
 {   
 fstream database;
 char filename[]="course.dat";

 int choice;
 do
    {
    showChoices();
    cin >> choice;
    switch (choice)
    {
    case 1:
            //  function call to write courses data
            writeCourse(database, filename);
            break;

    case 2:
            // call function 
            break;
    case 3:
            // call function 
            break;
    case 4:
            // call function 
            break;
    case 5:
            // call function 
            break;
    case 6:

        exit(1);
            break;
    default:
        cout<<"invalid input"<<endl;
    }

    }while (choice !=6);

     getchar();
     return 0;
     }


     void showChoices()
      {
      cout<< "Menu"<< endl;
      cout<< "1 : Enter Courses Data"<< endl;
      cout<< "2 : Enter ...."<< endl;
      cout<< "3 : Enter....."<< endl;
      cout<< "4 : Display ..."<< endl;
      cout<< "5 : Display Course Data"<< endl;
      cout<< "6 : Exit"<< endl;
      cout<< "Enter your choice:";
      }


     void writeCourse(fstream& aFile, char filename[])
          {
              Course courseData= {};//initialization
              aFile.open(filename,std::ios::out);
                 if (!aFile)
                  {
                   cout << "file opened for writing failed" << endl;
                   exit (-1);
                   }
              cout << endl;
              cout << "Begin Writing of " << filename << endl;
          cout << "Enter course ID, title, and creadit Hour\n? " << endl;


         for (int count=0; count<3;count++)
              {
          cin >>courseData.cID>>courseData.title>>courseData.creaditHour;
              }
         aFile.close();
         cout << "File name " << filename << " closed" << endl;
         cout << endl;
         }

2 个答案:

答案 0 :(得分:0)

因为您输入了要存储的错误数据类型。代码没有错。

courseDate.creadithour是来自int结构的Course,因此当您输入char类型时,会出现错误循环。

我的方法是将所有内置类型从Course结构更改为字符串,并确定输入是数字还是字母。

答案 1 :(得分:0)

在函数writeCourse()中,您需要删除输入流周围的for循环,以便为每个条目输入每个字段一次。此外,您需要将输出写入文件course.dat,以便将每个输入的记录保存在该文件中。使文件打开模式为&#34;追加&#34;这样所有条目都将被保存。代码如下:

void writeCourse(fstream& aFile, char filename[])
{
    Course courseData;//initialization
    aFile.open(filename, std::fstream::out | std::fstream::app);
    if (!aFile)
    {
        cout << "file opened for writing failed" << endl;
        exit (-1);
    }
    cout << endl;
    cout << "Begin Writing of " << filename << endl;
    cout << "Enter course ID, title, and creadit Hour\n" << endl;

    cin >> &courseData.cID[0] >> &courseData.title[0] >> courseData.creaditHour;
    aFile << &courseData.cID[0] << endl;
    aFile << &courseData.title[0] << endl;
    aFile << courseData.creaditHour << endl;
    aFile.close();
    cout << "File name " << filename << " closed" << endl;
    cout << endl;
}