2013-02-11 48 views
0

我正在嘗試從一個二進制文件的一些數據,把它們放在一個鏈表,這裏是我的代碼寫入文件:從二進制文件,無感人物檢索數據

void Pila::memorizzafile() 
{ 
    int contatore = 0; 
    puntarec temp = puntatesta; 
    ofstream miofile; 
    miofile.open("data.dat" , ios::binary | ios::out); 
    if(!miofile) cerr << "errore"; 
    else 
    { 
       while(temp) 
       { 
          temp->elem.writes(miofile); 
          contatore++; 
          temp = temp->next; 
       } 
       //I go back at the beginning of the file to write how many elements I have 
       miofile.seekp(0, ios::beg); 
       miofile.write((const char *)&contatore , sizeof(int)); 
       miofile.close(); 
    } 
} 

而功能寫道:

void Fiche::writes(ofstream &miofile) 
{ 
    //Valore. 
    miofile.write((const char *)&Valore,sizeof(int)); 
    //Materiale, I write the dimension of the string. 
    int buff = strlen(Materiale); 
    miofile.write((const char *)&buff,sizeof(int)); 
    //Writing the string 
    miofile.write(Materiale,buff*sizeof(char)); 
    //Dimension of Forma 
    buff = strlen(Forma); 
    miofile.write((const char*)&buff,sizeof(int)); 
    //The string itself 
    miofile.write(Forma,buff*sizeof(char)); 
    //Dimension of Colore. 
    buff = strlen(Colore); 
    miofile.write((const char*)&buff,sizeof(int)); 
    //The string 
    miofile.write(Colore,buff*sizeof(char)); 
} 

現在的閱讀部分,我試圖讓這應該是能夠直接從文件中讀取一個構造函數,那就是:

Pila::Pila(char * nomefile) 
{ 
    puntatesta = 0; 
    int contatore = 0; 
    ifstream miofile; 
    miofile.open(nomefile , ios::binary | ios::in); 
    if(!miofile) cerr << "errore"; 
    else 
    { 
     //I read how many records are stored in the file 
     miofile.read((char*)&contatore,sizeof(int)); 
     Fish temp; 
     for(int i = 0; i < contatore; i++) 
     { 
       temp.reads(miofile); 
       push(temp); 
     }    
     miofile.close(); 
    } 
} 

而且閱讀功能:

void Fiche::reads(ifstream &miofile) 
{ 
    //I read the Valore 
    miofile.read((char*)&Valore,sizeof(int)); 
    //I create a temporary char * 

    char * buffer; 
    int dim = 0; 
    //I read how long will be the string 
    miofile.read((char*)&dim,sizeof(int)); 
    buffer = new char[dim]; 
    miofile.read(buffer,dim); 
    //I use the set function I created to copy the buffer to the actual member char* 
    setMateriale(buffer); 
    delete [] buffer; 
    //Now it pretty much repeats itself for the other stuff 
    miofile.read((char*)&dim,sizeof(int)); 
    buffer = new char[dim]; 
    miofile.read(buffer,dim); 
    setForma(buffer); 
    delete [] buffer; 
    //And again. 
    miofile.read((char*)&dim,sizeof(int)); 
    buffer = new char[dim]; 
    miofile.read(buffer,dim); 
    setColore(buffer); 
    delete [] buffer;  
} 

代碼不給我任何錯誤,但在屏幕上我讀到隨機字符,而不是甚至遠程接近我在我的文件中寫道。任何人都可以幫我解決問題嗎?

編輯:

如這裏要求輸入&輸出的例子:

Fiche A("boh" , 4 , "no" , "gaia"); 
Fiche B("Marasco" , 3 , "boh" , "nonnt"); 
Fiche C("Valori" , 6 , "asd" , "hey"); 
Fiche D("TipO" , 7 , "lol" , "nonloso"); 
Pila pila; 
pila.push(A); 
pila.push(B); 
pila.push(C); 
pila.push(D); 
pila.stampa(); 
pila.memorizzafile(); 

和:

Pila pila("data.dat"); 
pila.stampa(); 
+0

你能舉一個輸入和輸出的例子嗎? – 2013-02-11 16:02:08

+1

寫入是「覆蓋」過程而不是「插入」。所以,當你做'miofile.seekp(0,ios :: beg);'然後寫你覆蓋第一個對象(除非你明確地留下了空間)。一次運行列表以獲取大小可能會更快。寫下來,然後寫下這些元素。 – 2013-02-11 16:04:05

+0

這是它!謝謝洛基!非常! – arocketman 2013-02-11 16:08:40

回答

1

這可能是你的錯誤:

  //I go back at the beginning of the file to write how many elements I have 
      miofile.seekp(0, ios::beg); 
      miofile.write((const char *)&contatore , sizeof(int)); 
      miofile.close(); 

通過尋求開始,然後寫作。你正在覆蓋第一個對象的一部分。

我認爲你最好的選擇是先完成列表並計算元素數。寫這個然後繼續寫所有的元素。無論如何,它可能會更快(但你可以花時間確定)。

我認爲你正在使用許多C結構來容納東西。

另外我會反對二進制格式的建議,除非你保存了大量的信息。文本格式(對於您的數據)可能會一樣好,並且會是人類可讀的,因此您可以查看該文件並查看出了什麼問題。

+0

這正是問題所在!我解決了這個問題,因爲它相對簡單,它的功能就像一個魅力!我非常感謝我可能從未見過錯誤!非常感謝你! – arocketman 2013-02-11 16:14:16