balaji
balaji

Reputation: 1125

how to use linux command in c++?

i am writing a c++ function that will parse .xml file. i Used system("grep"); type system call to use linux command. now my problem is that i want to select more than one line in given tag. ex.

content of .xml file
====================
<state>
 <text> hi this is first li </text>
 <sec> dsfn  sdlkf sfd </sec>
</state>
===================

i want to print all content between and tag. which and how i can use linux commands. plz help me....thanks i advance.

Upvotes: 0

Views: 2286

Answers (3)

Deve
Deve

Reputation: 4648

Use the C functions popen() and pclose()

Here's an example of how to use them:

#include <string>
#include <cstdio>
#include <errno.h>
#include <fstream>

int execShellCommand( const std::string command,
                      std::string& commandOutput )
{
  FILE *fp;
  char charBuf;
  std::ostringstream stdoutBuffer;
  int status;
  std::ostringstream errmsg;

  // Execute command and save its output to stdout
  fp = popen( command.c_str( ), "r" );
  if ( fp == NULL ) {
    // error handling
  } else {
    while ( ( charBuf = fgetc( fp ) ) != EOF ) {
      stdoutBuffer << charBuf;
    }
  }

  // Wait for completion of command and return its exit status
  status = pclose( fp );
  if ( status == -1 ) {
    // error handling
  } else {
    commandOutput = stdoutBuffer.str( );
    return WEXITSTATUS( status ); // convert exit status
  }
}

int main(){
  std::string command = "l | grep lib";
  std::string commandOutput;

  int exitCode = execShellCommand( command, commandOutput);
}

Upvotes: 1

sehe
sehe

Reputation: 392833

exec, execve or system calls: demo

#include <stdlib.h>
#include <unistd.h>

int main(int argc, const char* args[])
{
    exitcode = system("xsltprox textonly.xslt input.xml > output");
//  exitcode = exec("xsltproc", "textonly.xslt", "input.xml");
    int exitcode;
}

Proceed to fopen output (or std::fstream reader("output") if you fancy). Note that system is quickly a security hole, so you shouldn't use it in critical apps (like daemons). Using exec you could redirect. So you could perhaps open a pipe() from your program, fork(), in the child assign the stdout filedescriptor to the pipe() (see dup2 call) and exec in the child.

The parent process can then read the output from the pipe without need for a temp file. All of this should be on a zillion tutorials, which I don't have to the time to google for you right now.


May I suggest using perl or shell script. If you post an actual example I could show you the code in both shell and c/c++

Update here is how to do it with XSLT

textonly.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/">
        <xsl:for-each select="//*[text()]">
            <xsl:if test="text()">
                                <xsl:value-of select="text()"/>:
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

output demo:

o399hee@OWW00837 ~/stacko
$ xsltproc.exe textonly.xslt input.xml

 :
             hi this is first li :
             dsfn  sdlkf sfd :

Upvotes: 1

Adrien Rey-Jarthon
Adrien Rey-Jarthon

Reputation: 1154

Parsing XML using grep is really not a good idea, you should use some c++ xml parsing library like this one: http://xerces.apache.org/xerces-c/index.html

Upvotes: 4

Related Questions