Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question [C/CPP] Can this be done? Replacing words..

    I have an ASCII file input.sql which contains a database dump. I want that all the 5 lettered words that fall between a left parentheses i.e. ( and a comma i.e. , be replaced by the string (NULL,

    I did that with ease in PHP, but in c/cpp, it seems difficult.

    Here's the PHP code:

    PHP Code:
    <?php
    $blah
    =file_get_contents("input.sql");
    $blah1=preg_replace('/\(.....,/''(NULL,'$blah);
    $blah2=fopen("output.sql""a+");
    fwrite($blah2$blah1);
    ?>
    Any ideas people? I cant figure it out..

    Thanks.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    It's easily done in C++ with boost::regex_replace

    It's also rather easily done in either language using a state machine. Just loop the characters and start counting at each left paren. When the next comma comes up (or the count gets too high) replace if the count indicates the pattern has been matched, otherwise reset and carry on looking for the next paren.
    Last edited by ralph l mayo; 07-13-2009 at 06:27 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •