2016-08-17 98 views
-3

我一直工作到文件系統上,一切都看起來不錯,但如何將結構數據插入文件?

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

    } 

爲什麼我收到錯誤:不對應的「運營商< < '收藏< < CTR' 中|錯誤?該代碼在系統的早期階段工作,它只是沒有工作。

+2

此代碼可能非常多[mcve](http://stackoverflow.com/help/mcve)。嘗試將其修剪到儘可能少的行,這仍然會重現您的問題。 **幷包含編譯器的完整錯誤信息。** –

+2

''\ t「student'在您的字符串文字和下一個值之間缺少'<<'。這在你提問的線上非常猖獗。 – WhozCraig

回答

2

collection定義爲ifstream,即,即「輸入文件流」。它只支持一個文件,因此只有operator>>

您需要一個ofstream來輸出數據一個文件。或者你使用一個支持輸入和輸出的fstream。

而且WhozCraig已經指出它:您的代碼在幾個輸出數據之間缺少對operator<<的調用。

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;