本文介绍了如何将struct对象的输入数据插入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究这个代码,一切都很好,直到编译器给我一个错误 C:\ Users\dcst\Desktop\file\main.cpp | 108 |错误:否匹配'collection<<'in'collection<< ctr'| 如果我不能正确使用这种方法,我就无法继续处理其他事情。







I've been working on this code, everything's been fine until the compiler gave me an error C:\Users\dcst\Desktop\file\main.cpp|108|error: no match for 'operator<<' in 'collection << ctr'| and I can't continue working on the other things if I can't do this method right.



#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){
        default:
            cout<<"Invalid input."<<endl;
            system("pause");
            choice=4;
            break;
        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{
                if(collection.is_open()){
                    cout<<"Active File: ["<<filename<<"]";
                    system("cls");
                    while(getline(collection,lines)){
                    cout<<lines<<endl;
                    }
                }
                collection.close();
                collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);


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

                if(menu=='A'){
                    string lines2;
                    int one;
                    int two;
                    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;
                            }
                        }
                    collection.close();

                    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;

                    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();
                    system("pause");
                    ctr++;
                    collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);

                }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{
                    system("pause");
                };
            }while(menu!='X');
            break;
        }
    }while(choice!=4);

    }





我的尝试:



我试过这样做



collection.open(filename,std :: fstream :: in | std :: fstream :: out | std :: fstream :: app);

collection<< ctr<<\t<< student [ctr] .studentID<< \\t <<学生[CTR] .surname<< \t <<学生[CTR] .firstname<< \t<<学生[CTR] .birthdate< <\t<< student [ctr] .sex<< endl;

collection.close();



但我一直收到错误



What I have tried:

I've tried to do this

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();

but I keep getting that error

推荐答案

ifstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
// ...
collection.close();
// ...
collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
// Output operator << not present with istream based classes
collection<<ctr; // ...



要使用一个流执行输入和输出,请使用 fstream 类。但在您的情况下,最好使用新的输出流对象进行写入。


To perform input and output with one stream use the fstream class. But in your case it might be better to use a new output stream object for writing.


这篇关于如何将struct对象的输入数据插入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 00:08