2017-12-27 266 views
0

我一直用這段代碼停留在同一個地方。最後決定在網上提問。任何幫助,將不勝感激。從C++中的文本文件讀取/寫入結構

我已經創建了一個結構體,我可以將數據添加到結構中,但仍不確定我是否遵循了正確的方法。當我嘗試從文本文件讀取數據時,主要問題在於。

我似乎得到一個錯誤說:

錯誤C2678:二進制「>>」:沒有運營商發現,這需要左手 數類型的「的std :: ifstream的」(或有沒有可接受的轉化率)

結構體:

struct bankDetails    //structure for bank details 
{ 
    int acc_number; 
    double acc_balance; 
    double deposit_amt; 
    double withdraw_amt; 
    double interest_rate; 
    //char acc_type; 


}; 


struct CustDetails      //structure for account details 
{ 
    string cust_name; 
    string cust_pass; 
    bankDetails bankAccounts[99]; 
}; 

這是我寫從文件中讀取的代碼。

CustDetails loadDataFromFile() 
{ 
    CustDetails x; 
    ifstream dimensionsInfile; 
    dimensionsInfile.open ("storage.txt"); 
    for (int i=0; i < 2; i++)                
    { // write struct data from file 
     dimensionsInfile>> 
     &x.bankAccounts[i].acc_balance>> 
     &x.bankAccounts[i].acc_number>> 
     &x.cust_nam>> 
     &x.cust_pass>> 
     &x.bankAccounts[i].withdraw_amt>> 
     &x.bankAccounts[i].deposit_amt>> 
     &x.bankAccounts[i].interest_rate>> 
     cout<<"Data loaded"<<endl; 
    } 
    return x; 

} 

的代碼寫入到文件:主要功能

void details_save(int num,CustDetails x) 
{ 

    string filePath = "storage.txt";                    
    ofstream dimensionsOutfile;                  

    dimensionsOutfile.open ("storage.txt");            
    if (!dimensionsOutfile) 
     { 
      cout<<"Cannot load file"<<endl; 
      return ; 
     } 
    else 
     { 
      for (int i=0; i < num; i++)                
       { // write struct data from file 
        dimensionsOutfile<< 
        &x.bankAccounts[i].acc_balance<< 
        &x.bankAccounts[i].acc_number<< 
        &x.cust_name<< 
        &x.cust_pass<< 
        &x.bankAccounts[i].withdraw_amt<< 
        &x.bankAccounts[i].deposit_amt<< 
        &x.bankAccounts[i].interest_rate<< 
        cout<<" Customer 1 stored"<<endl; 
       } 
      cout <<"All details have been successfully saved"<<endl; 
      dimensionsOutfile.close(); 
     } 

} 

部分:

#include "stdafx.h" 
#include <string> 
#include <string.h> 
#include <ctime> 
#include <fstream> 
#include <sstream> 
#include <iostream> 
#include <iomanip> 

int main() 

{ 
    int maxNum; 
    CustDetails c; 
    c = loadDataFromFile(); //loads data from the file 

    { 
      //This part adds and changes values 
    } 

    details_save(maxNum, c); //saves data back to the file 
     return 0; 
} 

我在C++初學者任何幫助,將不勝感激。 乾杯!

+1

您將指針保存在文件中而不是實際數據中。 – drescherjm

+0

直接的問題是您正在通過指針將參數傳遞給各個字段。刪除'&':輸入opwrators通過引用獲取值。 ......最重要的是:你總是**需要在讀取操作後檢查**是否成功(如果你剛剛學習了最後一點,你剛剛做出了巨大的飛躍)。 –

回答

0

在loadDataFromFile()之前

cout<<""; 

更換 '>>' 有 ';'。

我們不能在cout中使用>>操作符。

雖然有更好更簡單的方法來做你正在做的事情。 fstream.h具有直接寫入和讀取類或結構對象的功能。

這裏的語法:

<stream name>.read((char*)<object name>,sizeof(<struct/class name>)); 

所以你loadDataFromFile可以簡化爲:

CustDetails loadDataFromFile() 
{ 
CustDetails x; 
ifstream dimensionsInfile; 
dimensionsInfile.open ("storage.txt");                
    // write struct data from file 
    dimensionsInfile.read((char*) CustDetails, sizeof(x)); 
    cout<<"Data loaded"<<endl; 
    return x; 
} 

同樣的Write函數的定義。它會爲你節省很多時間和麻煩。

0

閱讀關於file formats。你可能需要指定你的(在紙上......),那麼你可以使用該規範的一些EBNF表示法。

您目前的代碼可能是濫用operator >>operator <<。你不想在文件上寫指針(即使你在技術上是可以的),因爲再次讀它們是沒有意義的(因爲ASLR,並且由於不能保證地址從一次運行到下一次,或者從一次可執行文件保持不變到你的計劃明天有所改善)。

你可能想要做一些二進制IO(但我不建議這樣做)。然後,您將使用一些二進制輸入功能(如std::istream::read)來讀取記錄(例如某些PODstruct)。但是您不能(有意義地)在包含非POD數據的複雜類(如CustDetails)上執行二進制IO,例如std::string-s。實際上,數據往往比讀寫軟件更重要。因此,有一些更靈活的數據格式是有意義的(例如,您希望在兩年內能夠使用改進版本的代碼讀取某些舊代碼的版本的數據)。

因此,通常最好使用一些文本格式(並且您需要定義和記錄它)。您可以選擇一些現有的,如JSON,YAML,XML,S-expressions。你會發現很多庫可以幫助你(例如用於JSON等的jsoncpp等)。或者你也可以使用你自己的技術parsing

你也可以考慮一些database,也許就像sqlite一樣簡單。

另請參閱關於serializationapplication checkpointingpersistenceportability