代码之家  ›  专栏  ›  技术社区  ›  adam101

c中文本文件的读/写结构++

  •  0
  • adam101  · 技术社区  · 7 年前

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

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

    我似乎在说:

    错误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++的初学者,如有任何帮助,将不胜感激。 干杯

    2 回复  |  直到 7 年前
        1
  •  1
  •   Basile Starynkevitch    7 年前

    阅读关于 file formats . 您可能需要指定您的(书面……),那你可以用一些 EBNF 该规范的符号。

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

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

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

    因此,通常最好使用一些文本格式(您需要定义并记录它)。您可以选择一些现有的,例如 JSON , YAML , XML , S-expressions . 您会发现许多库可以帮助您(例如。 jsoncpp 对于JSON等…)。或者你也可以用你自己的 parsing 技术。

    你也可以考虑一些 database ,可能很简单 sqlite .

    另请阅读关于 serialization , application checkpointing , persistence portability .

        2
  •  0
  •   deepjyoti30    7 年前

    在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;
    }
    

    类似地,写入函数的写入定义。这将为你节省大量的时间和麻烦。