find substring & replace string

Aug 19, 2017 at 1:33pm
String like this:

some thing:200 // some sentence

I can find position of ":".

But now I need to get the string "200" which is after the : and before a space.
This is step one.

1
2
3
getline (ifs, line);
found = line.find(':',3);
std::cout << line << std::endl;


The next step I need to do is to remove the "some thing:" from the line.

Any suggestion how to do it?
Last edited on Aug 19, 2017 at 1:49pm
Aug 19, 2017 at 2:14pm
1
2
const auto found_at = line.find( ":200 " ) ; // find "200" which is after the : and before a space.
if( found_at != std::string::npos ) line.erase( 0, found_at+1 ) ; // erase the : and everything before it 
Aug 19, 2017 at 2:33pm
Thank you. One more question. How to duplicate string? copy

For this purpose:
1
2
3
4
std::string cpy(line); // how to duplicate line?
line = "<h2>";
line += cpy;
line += "</h2>";


Trying to avoid use of .to_string because of buggy minGW
Last edited on Aug 19, 2017 at 2:34pm
Aug 19, 2017 at 2:38pm
We don't need to duplicate the string for this. line = "<h2>" + line + "</h2>"; would suffice.
Aug 19, 2017 at 2:49pm
unfortunatle this part crashes the program (maybe) - the debugger stops to work or sends SIGSEGV signal.

line = "<h2>" + line + "</h2>";

It looks like the second + operator (in the leftwards direction) is not processed .

SIGSEGV is fired only when watching line. Otherwise when I use F8 to debug, it looks like working. But with debuging step by step it will not pass through.
Last edited on Aug 19, 2017 at 2:55pm
Aug 19, 2017 at 2:54pm
hmm... what happens if you run the program by itself (without gdb)?

And what about this (under gdb)?
1
2
const std::string cpy = line ;
line = "<h2>" + cpy+ "</h2>";

Aug 19, 2017 at 3:05pm
I think the problem is elsewhere in the code. I did not follow your code exactly.

Now when I turn back to test your code exactly, first problem I have is:

const auto found_at;

warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
error: 'found_at' does not name a type|
error: 'found_at' was not declared in this scope|

In the code

1
2
const auto found_at = line.find(':',3);
line.erase( 0, found_at+1 );
Aug 19, 2017 at 3:56pm
Enable C++11 : see: http://www.cplusplus.com/doc/tutorial/introduction/codeblocks/
In addition to -std=c++11, also select -Wall -Wextra -pedantic-erorrs


Or in legacy C++:
1
2
3
// const auto found_at = line.find( ":200 " ) ; // find "200" which is after the : and before a space.
const std::size_t found_at = line.find( ":200 " ) ; // find "200" which is after the : and before a space.
if( found_at != std::string::npos ) line.erase( 0, found_at+1 ) ; // erase the : and everything before it  
Aug 19, 2017 at 4:05pm
I have found this:

1
2
line = "</h2>" + line + "</h2>";
std::cout << line << std::endl;


This will stop the debugger - however maybe there is not error.

This will not stop the debugger but it will send SIGSEGV signal
1
2
3
4
5
sprintf(iStr, "%s", line.c_str() );
line += "<h2>";
line += iStr;
line += "</h2>";
getline (ifs, line);


I did not know what it means, but now I see it happens only when I watch the line. If I do not watch it, it will not send the SIGSEGV

3. it depends there I declare line. If I add it outside while loops on the begin of program, it will start to send SIGSEGV signal very early (because?) line was not initiated (?). This behaviour is strange. So I moved the declaration into while getline loop ... so the SIGSEGV is not displayed so early. It sucks because everytime it is displayed I see Stack Call window which takes 1/3 of my screen... Quite not comfortable debugging.

I am gonna to check your link now.

OK, I did your suggestion and checked the C++11. I did not know I can enable it! Thanks for it.

Now, when I have checked the compiler options it does not break here:
line = "<h2>" + line + "</h2>";

IDK what is going on here.

Do you know how to set hotkey for the Call Stack Window?
Last edited on Aug 19, 2017 at 4:17pm
Aug 19, 2017 at 4:10pm
> This will not stop the debugger but it will send SIGSEGV signal
> sprintf(iStr, "%s", line.c_str() );

Most likely, buffer overflow on the array iStr
Why are you messing around with C-style arrays?
Aug 19, 2017 at 4:26pm
One more thing I need to find out to do.

How to save the previous line to use it later to replace previous line?

could this work?

1
2
prevLine = line;
getline (ifs, line);


Why are you messing around with C-style arrays?

Because I could not pass through the debugger break. I replaced the code to make it working. Now I use this:
line = "<h2>" + line + "</h2>";
and this:
1
2
3
4
5
c++;
sprintf(iStr, "%d", c);
line += "<h3>";
line += iStr;
line += "</h3>"; // I cannot do this: const char[4] + char[40] + const char[5] 
Last edited on Aug 19, 2017 at 4:31pm
Aug 19, 2017 at 4:29pm
Yes. (Assuming that the type of prevLine is std::string)
Topic archived. No new replies allowed.