How replace all ocurrences in std::string?

Jan 24, 2014 at 1:47am
I'm trying to change C++ code to HTML, I made an tool, and I really need this. The program is just for me, not need-ing to allocate memory and other programming good pratices... But I can't find the real error int this simple code!?! Why it doesn't works?
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
#include <fstream>
#include <string>
void ReplaceStringInPlace(std::string& subject, const std::string& search,
                          const std::string& replace) {
    size_t pos = 0;
    while ((pos = subject.find(search, pos)) != std::string::npos) {
         subject.replace(pos, search.length(), replace);
         pos += replace.length();
    }
}

using namespace std;
int main(int argc, char* argv[])
{
    string str;
    ifstream read;
    read.open(argv[1]);
    while(!read.eof())
    {
        read >> str;
    }
    read.close();
    ReplaceStringInPlace(str,"<","&lt;");
    ReplaceStringInPlace(str,">","&gt;");
    ReplaceStringInPlace(str,"\n","<br>");
    ofstream file;
    file.open(argv[1], ios::trunc);
    file << str;
    file.close();
}
Jan 24, 2014 at 3:21am
You need to explain more than "it doesn't work". Does it blow up your next-door neighbor's house? Does it start a grease fire? Does it embezzle federal funds? You need to be specific.
Jan 24, 2014 at 10:40am
When going to use, too much data is lost.
Jan 24, 2014 at 10:44am
Make sure str has got the correct value.
Jan 24, 2014 at 1:31pm
I switched the string by a char. Now, I'm getting a runtime error while going to create the other file (as gdb.exe said, in function extension_ocurrence(), just the renaming, but don't know why and how to fix :|). My code is
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <fstream>
#include <sstream>
#include <cstring>
#include <iostream>
using namespace std;
size_t extension_ocurrence(const char* str)
{
    size_t ocurrence;
    for(ocurrence = strlen(str); str[ocurrence] != '.'; ocurrence--);
    return ocurrence;
}
char* get_extension(const char* str)
{
    unsigned ocurrence = extension_ocurrence(str);
    unsigned ext_len;
    char* str_stack = new char[strlen(str) - ocurrence];
    for(ext_len = ocurrence; ext_len <= strlen(str); ext_len++)
    {
        str_stack[ext_len - ocurrence] = str[ext_len];
    }
    return str_stack;
}
char* remove_extension(const char* str)
{
    char* ret_ = new char[extension_ocurrence(str)];
    for(size_t i = 0; i < extension_ocurrence(str); i++)
    {
        ret_[i] = str[i];
    }
    return ret_;
}

int main(int argc, char* argv[])
{

    ifstream read;
    read.open(argv[1]);
    stringstream main_str;
    /* add _HTMLify to file name */
    main_str << remove_extension(argv[1]) << "_HTMLify" << get_extension(argv[1]);
    ofstream out;
    out.open(main_str.str(), ios::trunc);
    char str;
    while(!read.eof())
    {
        read >> str;
        switch(str)
        {
            case '<':
                out << "&lt;";
            case '\n':
                out << "<br>";
            case '>':
                out << "&gt;";
            case '&':
                out << "&amp;";
            default:
                out << str;
        }

    }
    read.close();
    ofstream file;
    file.open(argv[1], ios::trunc);
    file << str;
    file.close();
}
Jan 24, 2014 at 2:10pm
The extension_ocurrence function will only work if the string contains a dot.
Jan 24, 2014 at 2:22pm
And it contains dot!
Jan 24, 2014 at 3:48pm
In the get_extension function the size of the allocated array is one too small.

In the remove_extension function you forgot to set the null terminator of the ret_ string.
Last edited on Jan 24, 2014 at 3:56pm
Jan 24, 2014 at 4:29pm
What about using a regex ? I've seen enough C++11 code on these forums, so why you don't use it ?

Or you can copy-paste this code (or learn how it works) :)
http://www.zedwood.com/article/cpp-str_replace-function
Last edited on Jan 24, 2014 at 4:31pm
Jan 24, 2014 at 6:07pm
Why are you not using std::string?

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

std::size_t extension_occurrence(const std::string& s)
{
    return s.find_last_of('.');
}

std::string get_extension(const std::string& s)
{
    std::size_t pos = extension_occurrence(s);
    return pos == std::string::npos ? std::string() : s.substr(pos);
}

std::string remove_extension(std::string s)
{
    std::size_t pos = extension_occurrence(s);
    
    if (pos != std::string::npos)
        s.erase(pos);

    return s;
}

std::string htmlify(const std::string& s)
{
    return remove_extension(s) + "_HTMLify" + get_extension(s);
}

void replace_symbols(std::string & s)
{
    const char* symbols = "><&";

    std::size_t pos = 0;
    while ( pos != s.size() && (pos = s.find_first_of(symbols, pos)) != std::string::npos)
    {
        const char* rep = "";
        switch (s[pos])
        {
        case '>': rep = "&gt;"; break;
        case '<': rep = "&lt;"; break;
        case '&': rep = "&amp;"; break;
        default: break;
        }

        s.replace(pos, 1, rep);
        pos += std::strlen(rep);
    }
}

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        std::cerr << "Not enough arguments.\n";
        return 0;
    }

    std::ifstream in(argv[1]);
    std::string out_name = htmlify(argv[1]);
    std::ofstream out(out_name);

    std::string line;
    while (std::getline(in, line))
    {
        replace_symbols(line);
        out << line << "<br>\n";
    }

    //in.clear();
    //in.close();
    //out.close();

    //in.open(out_name);
    //out.open(argv[1]);

    //out << in.rdbuf();
}
Jan 24, 2014 at 8:07pm
Thank you so much @cire! Solved!
Jan 25, 2014 at 3:13am
what does the program do?
Jan 25, 2014 at 3:17am
Becasue its not working on my linux ubuntu compiler
Jan 25, 2014 at 3:44am
what does the program do?

It replaces newlines, less than symbols and greater than symbols with the equivalent html entity name. It would probably be simpler to use a <code></code> container...


Becasue its not working on my linux ubuntu compiler

Since you didn't know what the program did, how do you know it's not working? Is it not compiling? If it isn't, does your compiler not give you a listing of errors/warnings? If it is compiling, what makes you think it doesn't work?
Jan 25, 2014 at 10:29am
@cire <code></code> tags doesn't work. They only let the style, but C++ code became HTML code, then the browser lose it.
Topic archived. No new replies allowed.