C++ 如何使用C++;

C++ 如何使用C++;,c++,file,C++,File,我正在打开一个csv文件,其中包含“key,value”形式的条目。 任务是找到一个特定的键,并用另一个字符串修改其对应的值。(键和值:都是字符串) 不能使用seekg()和tellg()函数直接修改文件,除非这些函数具有要更改的零件的固定大小格式 通常的方法是创建一个中间临时文件来写入您的更改,并在完成后用它替换原来的文件。配方: #include <cstdlib> #include <iostream> #include <utility> #inclu

我正在打开一个csv文件,其中包含“key,value”形式的条目。 任务是找到一个特定的键,并用另一个字符串修改其对应的值。(键和值:都是字符串)


不能使用
seekg()
tellg()
函数直接修改文件,除非这些函数具有要更改的零件的固定大小格式

通常的方法是创建一个中间临时文件来写入您的更改,并在完成后用它替换原来的文件。

配方:

#include <cstdlib>
#include <iostream>
#include <utility>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>

class pair : public std::pair<std::string, std::string>
{
public:
    using std::pair<std::string, std::string>::pair;
    bool operator==(pair const & rhs) const { return first == rhs.first; }

    friend std::istream& operator>>(std::istream &is, pair &p)
    {
        std::getline(is, p.first, ',');
        std::getline(is, p.second, '\n');
        return is;
    }

    friend std::ostream& operator<<(std::ostream &os, pair const &p)
    {
        os << p.first << ',' << p.second << '\n';
        return os;
    }
};

int main()
{
    char const *filename = "input.csv";
    std::fstream file{ filename, std::ios::in };
    if (!file) {
        std::cerr << "Failed to open file \"" << filename << "\"!\n\n";
        return EXIT_FAILURE;
    }

    std::vector<pair> data{
        std::istream_iterator<pair>{ file },
        std::istream_iterator<pair>{}
    };
    file.close();

    auto it{ std::find(data.begin(), data.end(), pair{ "Black", "" }) };
    if (it == data.end()) {
        std::cerr << "Key not found!\n\n";
        return EXIT_FAILURE;
    }

    it->second = "Dreamers";

    file.open(filename, std::ios::out | std::ios::trunc);
    if (!file) {
        std::cerr << "Failed to open file \"" << filename << "\"!\n\n";
        return EXIT_FAILURE;
    }

    for (auto const & entry : data)
        file << entry;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类对:公共std::pair
{
公众:
使用std::pair::pair;
布尔运算符==(成对常量和rhs)常量{return first==rhs.first;}
friend std::istream&operator>>(std::istream&is,配对与编程)
{
std::getline(is,p.first,',');
std::getline(is,p.second,'\n');
回报是;
}

friend std::ostream&operator“Cats”与“Dreamers”的长度不同所以这个方法是行不通的。你可以把整个文件读入向量或地图,然后改变任何东西,然后把数据写回文件。如果你的要求不一定是C++,SED是一个更好的工具。试试:<代码> SED/IKE,Value/KEY,NeXOVald/G'文件< /C>这个解决方案起作用。所以,我将不得不加载文件中的整个内容。容器,然后再对其进行任何更改it@user3370378这只是一个可能的解决方案。你也可以自己处理每一行。是的,我已经这样做了。但这种方法的问题是,如果我的新行的内容大于我的旧行的内容,那么它会覆盖下一行(上面代码中提到的示例)
Star,Treks
Captain,America
BlBlack,Dreamers
Fighters
#include <cstdlib>
#include <iostream>
#include <utility>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>

class pair : public std::pair<std::string, std::string>
{
public:
    using std::pair<std::string, std::string>::pair;
    bool operator==(pair const & rhs) const { return first == rhs.first; }

    friend std::istream& operator>>(std::istream &is, pair &p)
    {
        std::getline(is, p.first, ',');
        std::getline(is, p.second, '\n');
        return is;
    }

    friend std::ostream& operator<<(std::ostream &os, pair const &p)
    {
        os << p.first << ',' << p.second << '\n';
        return os;
    }
};

int main()
{
    char const *filename = "input.csv";
    std::fstream file{ filename, std::ios::in };
    if (!file) {
        std::cerr << "Failed to open file \"" << filename << "\"!\n\n";
        return EXIT_FAILURE;
    }

    std::vector<pair> data{
        std::istream_iterator<pair>{ file },
        std::istream_iterator<pair>{}
    };
    file.close();

    auto it{ std::find(data.begin(), data.end(), pair{ "Black", "" }) };
    if (it == data.end()) {
        std::cerr << "Key not found!\n\n";
        return EXIT_FAILURE;
    }

    it->second = "Dreamers";

    file.open(filename, std::ios::out | std::ios::trunc);
    if (!file) {
        std::cerr << "Failed to open file \"" << filename << "\"!\n\n";
        return EXIT_FAILURE;
    }

    for (auto const & entry : data)
        file << entry;
}