简体   繁体   English

使用fstream写入文件

[英]Write into a file using fstream

I tried to write into a file with fstream using the following code. 我尝试使用以下代码使用fstream写入文件。 It accepts the first entry and when I hit Enter , it goes into an infinite loop to the menu instead of allowing me to enter my second and third entries. 它接受第一个条目,当我按Enter键时 ,它将进入菜单的无限循环,而不是允许我输入第二个和第三个条目。

How can I fix this? 我怎样才能解决这个问题?

#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;
         }

Because you input a wrong data type that you want to store. 因为输入的数据类型错误,所以要存储。 There's nothing wrong in the code. 代码没有错。

courseDate.creadithour is an int from the Course struct, so when you input a char type, error loop comes. courseDate.creadithourCourse结构的int ,因此当您输入char类型时,会出现错误循环。

My approach for this is to change all the built in type from the Course struct into string and determine the input whether it is a digit or a letter. 我的方法是将Course结构中的所有内置类型都更改为字符串,并确定输入是数字还是字母。

In the function writeCourse() , you need to remove the for loop around the input stream so that each field will be entered exactly once for each entry. 在函数writeCourse() ,您需要删除输入流周围的for循环,以便每个字段的每个字段将被准确输入一次。 Also, you need to write the output to the file course.dat so that each entered record is saved in that file. 另外,您需要将输出写入文件course.dat以便将每个输入的记录保存在该文件中。 Make the file open mode as "append" so that all entries will be saved. 将文件打开模式设置为“追加”,以便保存所有条目。 Code works as below: 代码如下:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM