如何将struct数据插入文件?

时间:2016-08-17 04:56:34

标签: c++

我一直在制作唱片系统,一切看起来不错,但

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>


using namespace std;

struct students{
    string studentID;
    string surname;
    string firstname;
    string birthdate;
    string sex;
};

int main()
{
    fstream collection;
    string filename;
    short choice;



    do{
        int ctr=1;
        system("cls");
        if(collection.is_open()){
                cout<<"Active File: ["<<filename<<"]"<<endl;
        }else{
                cout<<"Active File|: [None opened]"<<endl;
        }

        cout<<"[1] Create new file"<<endl;
        cout<<"[2] Open existing file"<<endl;
        cout<<"[3] Manage data"<<endl;
        cout<<"[4] Exit"<<endl;
        cout<<"Enter operation index: ";
        cin>>choice;
        switch(choice){
        case 1:
            cout<<"Enter file name: ";
            cin>>filename;
            collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
            collection<<"------------------------------------------------------------------------------"<<endl;
            collection<<"Rec \t Student ID \t Surname \t Firstname \t Birthdate \t Sex \t"<<endl;
            collection<<"------------------------------------------------------------------------------"<<endl;
            collection.close();
            collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
            break;
        case 2:
            cout<<"Enter file name: ";
            cin>>filename;
            collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
            break;
        case 3:
            string lines;
            char menu;
            students student[10];

            do{
                ifstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
                if(collection.is_open()){
                    cout<<"Active File: ["<<filename<<"]";
                    system("cls");
                    while(getline(collection,lines)){
                    cout<<lines<<endl;
                    }
                }
                collection.close();

                cout<<"[A]dd  [E]dit [D]elete  [S]ort  [F]ilter  Sa[V]e  e[X]it";
                cin>>menu;

                if(menu=='A'){
                    string lines2;
                    collection.open(filename,ios::app);
                    system("cls");

                    ifstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
                        if(collection.is_open()){
                            while(getline(collection,lines)){
                                cout<<lines<<endl;
                            }
                        }

                    cout<<endl<<"Adding data to "<<filename<<endl;
                    cout<<"Student ID: ";
                    cin>>student[ctr].studentID;
                    cout<<"Surname: ";
                    cin>>student[ctr].surname;
                    cout<<"Firstname: ";
                    cin>>student[ctr].firstname;
                    cout<<"Birthdate: ";
                    cin>>student[ctr].birthdate;
                    cout<<"Sex: ";
                    cin>>student[ctr].sex;

为什么我一直在这里收到错误?

                    //data insertion code heree
                    collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
                    collection<<ctr<<"\t"student[ctr].studentID<<"\t"student[ctr].surname<<"\t"student[ctr].firstname<<"\t"student[ctr].birthdate<<"\t"student[ctr].sex<<endl;

                    collection.close();
                    ctr++;
                }else if(menu=='E'){

                }else if(menu=='D'){

                }else if(menu=='F'){

                }else if(menu=='V'){
                    cout<<"Saving file..."<<endl;
                    collection.close();
                    cout<<"File saved."<<endl;
                    system("pause");
                }else{
                    cout<<"Invalid input."<<endl;
                    system("pause");
                };
            }while(menu!='X');
            break;
        }
    }while(choice!=4);

    }

为什么我收到错误:'operator&lt;&lt;'不匹配在'集合&lt;&lt; CTR'|错误?该代码在系统的早期部分工作,但它不起作用。

2 个答案:

答案 0 :(得分:2)

collection被定义为ifstream,i。即&#34;输入文件流&#34;。它仅支持从文件中读取输入,因此仅支持operator>>

您需要ofstream将数据输出到文件。或者你使用fstream,它支持输入和输出。

WhozCraig已经指出了这一点:您的代码在几个输出数据之间缺少对operator<<的调用。

答案 1 :(得分:2)

您在两个地方定义了collection,而在您使用它的位置是istream。您无法输出到ifstream。

你也忘记了一堆<< 这应该解决它。您也可以直接在构造函数中打开文件,这样会更好。

            fstream collection_out;
            collection_out.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
            collection_out << ctr << "\t" << student[ctr].studentID<<"\t" << student[ctr].surname<<"\t"<<student[ctr].firstname<<"\t" << student[ctr].birthdate<<"\t"<< student[ctr].sex<<endl;