当我尝试运行程序时,Visual Studio抛出如下错误:“错误:无法打开文件C:\ Users ... \ test1 \ Debug \ investment.obj。错误代码= 0x80070002。
我尝试了许多在线提到的方法,但是仍然无法正常工作。所以,我在想代码中是否有任何问题。
这是怎么回事?请帮忙。
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>
using namespace std;
struct Investment
{
string Name;
string BankAccount;
string SortCode;
float Investment;
float Contribution;
};
string Trim(string str)
{
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}
void Get_Data(const string& filename, vector<Investment>& data)
{
ifstream fin(filename);
if (!fin.is_open())
return;
string line;
if (!getline(fin, line))
return;
while (getline(fin, line))
{
istringstream sin(line);
vector<string> fields;
string field;
while (getline(sin, field, ','))
{
fields.push_back(field);
}
Investment inv;
inv.Name = Trim(fields[0]);
inv.BankAccount = Trim(fields[1]);
inv.SortCode = Trim(fields[2]);
inv.Investment = atof(Trim(fields[3]).c_str());
inv.Contribution = 0.0f;
data.push_back(inv);
}
}
void Save_Data(const string& filename, const vector<Investment>& data)
{
ofstream fout(filename);
if (!fout.is_open())
return;
fout << "NAME, BANK ACCOUNT, SORT CODE, INVESTMENT, Contribution\n";
for (auto& inv : data)
{
fout << inv.Name << " "
<< inv.BankAccount << " "
<< inv.SortCode << " "
<< inv.Investment << " "
<< inv.Contribution << "\n";
}
}
int main()
{
vector<Investment> Data_investment;
Get_Data("aaa.csv", Data_investment);
float total = accumulate(Data_investment.begin(), Data_investment.end(), 0.0f,[](float sum, const Investment& inv) { return sum + inv.Investment; }
);
for (auto& inv : Data_investment)
{
float percentage = (inv.Investment * 100.0f) / total;
inv.Contribution = percentage;
}
Save_Data("aaa_new.csv", Data_investment);
return 0;
}
答案 0 :(得分:0)
我已经将您的源代码成功地进行了很小的修改就编译到了Visual Studio 2017中。请看看。
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>
using namespace std;
struct Investment
{
std::string Name;
std::string BankAccount;
std::string SortCode;
float Invest; // Changed due the variable name goes same as struct name if use Investment
float Contribution;
};
string Trim(string &str)
{
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}
void Get_Data(const string& filename, vector<Investment>& data)
{
ifstream fin(filename);
if (!fin.is_open())
return;
string line;
if (!getline(fin, line))
return;
while (getline(fin, line))
{
istringstream sin(line);
vector<string> fields;
string field;
while (getline(sin, field, ','))
{
fields.push_back(field);
}
Investment inv;
inv.Name = Trim(fields[0]);
inv.BankAccount = Trim(fields[1]);
inv.SortCode = Trim(fields[2]);
inv.Invest = atof(Trim(fields[3]).c_str());
inv.Contribution = 0.0f;
data.push_back(inv);
}
}
void Save_Data(const string& filename, const vector<Investment>& data)
{
ofstream fout(filename);
if (!fout.is_open())
return;
fout << "NAME, BANK ACCOUNT, SORT CODE, INVESTMENT, Contribution\n";
for (auto& inv : data)
{
fout << inv.Name << " "
<< inv.BankAccount << " "
<< inv.SortCode << " "
<< inv.Invest << " "
<< inv.Contribution << "\n";
}
}
int main()
{
vector<Investment> Data_investment;
Get_Data("aaa.csv", Data_investment);
float total = accumulate(Data_investment.begin(), Data_investment.end(), 0.0f, [](float sum, const Investment& inv) { return sum + inv.Invest; }
);
for (auto& inv : Data_investment)
{
float percentage = (inv.Invest * 100.0f) / total;
inv.Contribution = percentage;
}
Save_Data("aaa_new.csv", Data_investment);
return 0;
}
下面是修改后的部分
std::string Name;
std::string BankAccount;
std::string SortCode;
float Invest; // Changed due the variable name goes same as struct name if use Investment