在c ++中从文本文件读/写结构

问题描述 投票:0回答:2

我用这段代码被困在同一个地方一段时间了。最后决定在网上询问。任何帮助,将不胜感激。

我已经创建了一个结构,我可以将数据添加到结构中,但仍然不确定我是否遵循正确的方法。主要问题在于我尝试从文本文件中读取数据。

我似乎得到一个错误说:

错误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 ++的初学者,非常感谢任何帮助。干杯!!

c++ visual-c++ struct iostream ifstream
2个回答
0
投票

在loadDataFromFile()之前

cout<<"";

用';'替换'>>'。

我们不能在cout中使用>> operator。

虽然有一种更好,更简单的方法来做你正在做的事情。 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;
}

类似地写写函数的定义。它将为您节省大量时间和麻烦。


0
投票

了解file formats。您可能需要指定您的(在纸上....),然后您可以使用一些EBNF表示法。

您当前的代码可能会滥用operator >>operator <<。你不想在文件上写指针(即使你在技术上可以),因为再次读取它们是没有意义的(因为ASLR,并且因为地址不能保证从一次运行到下一次运行或从一个可执行文件保持不变对你的程序明天略有改善)。

您可能想要做一些二进制IO(但我不建议这样做)。然后你会使用像std::istream::read这样的二进制输入函数来读取记录(例如一些POD struct)。但是你不能(有意义地)对包含非POD数据(如CustDetails-s)的复杂类(例如std::string)执行二进制IO。

实际上,数据通常比阅读和编写软件更重要。因此,有一些更灵活的数据格式是有意义的(例如,您希望能够在两年内让代码的改进版本读取由某些旧版本代码编写的某些数据)。

因此,通常最好使用一些文本格式(您需要定义和记录它)。你可以选择一些现有的,如JSONYAMLXMLS-expressions。你会发现很多图书馆可以帮助你(例如jsoncpp for JSON等等)。或者您也可以使用自己的parsing技术。

你也可以考虑一些database,也许像sqlite一样简单。

另请阅读serializationapplication checkpointingpersistenceportability

© www.soinside.com 2019 - 2024. All rights reserved.