2017-12-27 196 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