summaryrefslogtreecommitdiff
path: root/graphs/cpp/replace/replace.cc
blob: c24bf05fa1e018d52e38d6ae3a5cf2d71f2a4ce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "replace.hh"

#include <fstream>
#include <iostream>
#include <string>

void replace(const std::string& input_filename,
             const std::string& output_filename, const std::string& src_token,
             const std::string& dst_token)
{
    std::ofstream file_out;
    file_out.open(output_filename);

    if (!file_out.is_open())
    {
        std::cerr << "Cannot write output file\n";
        return;
    }

    std::ifstream file_in;
    file_in.open(input_filename);
    if (!file_in.is_open())
    {
        std::cerr << "Cannot open input file\n";
        return;
    }

    std::string token;
    while (std::getline(file_in, token))
    {
        std::string::size_type n = 0;
        while ((n = token.find(src_token, n)) != std::string::npos)
        {
            token.replace(n, src_token.size(), dst_token);
            n += dst_token.size();
        }
        file_out << token << "\n";
    }
}