Best way to manipulate texts using C++

How to manipulate texts using C++ like we do it using awk command in Unix

not sure i understand but...

all languages have strengths and weaknesses, AWK was designed for processing text so it feels really easy to do because it comes naturally to that language.

C/C++ was designed for processing integers, floats and characters, and has much finer detail.

There are powerful string libraries out there that you can use in C++ but they wont give you the power that AWK does. you will still have to manually break the strings up and manage the fragments.

I'd be researching regular expressions to do that stuff.
C++ is a compiled language. You write code that implements "manipulations", compile, and get optimized binary that you then use for "manipulating text".

AWK is an interpreter, an optimized binary. You feed it text and manipulation commands.

If you code into your C++ app specific manipulations, it is like writing one awk script.
If you code your C++ app to receive and parse manipulation commands, then you reinvent the awk.

That is not to say that C++ should not be used; C++ is a generic programming language and can implement text processing among other things.

What manipulations? There are probably a lot that awk can do (I resort to it for calculations and prefer sed for text). You have to be more specific about the actual problem before anyone can recommend "the optimal tool" for it.

"in Unix" ... Do you really have a platform, where you cannot use awk?
Last edited on
Thanks keskiverto. It will helpful for me.
> How to manipulate texts using C++ like we do it using awk command in Unix

We can invoke awk (or GNU's gawk) from within a C++ program.

For instance:
#include <iostream>
#include <string>
#include <cstdlib>

int main()
    const std::string gnu_awk = "/usr/bin/gawk" ; 
    const std::string file_name = __FILE__ ;
    const std::string awk_cmd = " '{ print toupper($0) }' " ;
    std::system( ( gnu_awk + awk_cmd + file_name ).c_str() ) ;

To capture the output of awk/gawk, we can use popen (Posix) or _popen (Microsoft).
There are many, more or less complicated ways to do it. Just try not to make it too complex because although there are programs that helps detecting bugs that are made by high complexity, as checkmarx it is better to try and avoid these issues with some simpler ways.
Topic archived. No new replies allowed.