我和这段代码一直呆在同一个地方。最后决定在网上询问。任何帮助,将不胜感激。
我已经创建了一个结构,我可以将数据添加到结构中,但仍然不确定我是否遵循正确的方法。主要问题在于我尝试从文本文件中读取数据。
我似乎得到一个错误说:
错误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 ++的初学者,我们非常感谢任何帮助。 干杯!!
答案 0 :(得分:0)
了解file formats。您可能需要指定您的(在纸上......),然后您可以对该规范使用一些EBNF表示法。
您当前的代码可能会误导operator >>
和operator <<
。你不想在文件上写指针(即使你在技术上可以),因为再次读取它们是没有意义的(因为ASLR,并且因为地址不能保证从一次运行到下一次运行保持不变,或者从一个可执行文件到你的程序明天稍微改进了。)
您可能想要做一些二进制IO(但我不建议这样做)。然后你会使用像std::istream::read
这样的二进制输入函数来读取记录(例如某些POD struct
)。但是你不能(有意义地)对包含非POD数据的复杂类(例如CustDetails
)执行二进制IO,例如std::string
- s。
实际上,数据通常比阅读和编写软件更重要。因此,有一些更灵活的数据格式是有意义的(例如,您希望能够在两年内使用改进的版本的代码读取某些旧的>> 您的代码版本。)
因此,通常最好使用一些文本格式(您需要定义和记录它)。您可以选择一些现有的,例如JSON,YAML,XML,S-expressions。您会发现许多库可以帮助您(例如jsoncpp用于JSON等等)。或者您也可以使用自己的parsing技术。
你也可以考虑一些database,也许就像sqlite一样简单。
另请阅读serialization,application checkpointing,persistence和portability。
答案 1 :(得分:0)
在
之前的loadDataFromFile()中cout<<"";
替换'&gt;&gt;'用';'。
我们无法使用&gt;&gt;操作员与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;
}
类似写入函数的定义。这将为您节省大量时间和麻烦。