繁体   English   中英

编译以下代码时发生错误

[英]Error occurred when I compile the following code

当我尝试运行程序时,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;
}

我已经成功地将您的源代码编译到 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM