Thread: File processing and tr command

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    Question File processing and tr command

    I'm trying to parse certain text files and it would be easier if the contents where lower case with no punctuation, etc. So I was curious if c++ had anything similar to the unix tr command?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    You can use tolower. Just don't forget to cast the output as a character since its return type is an integer. Or you could use it in a transform function adapter with a string type which wouldn't require you to do casting. Or you could use the two parameter version of tolower which takes the beginning and end pointers of a string.

    [edit]
    To get rid of the punctuation and stuff, however, you would have to write your own specific code. It would probably be easier on you just to use tr to preprocess the text then work with that.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    that would work fine for the lower casing, but I wanted to do some other formatting as well in one big swoop, like tr allows...I might have to just parse it char by char though

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I'm afraid c++ is not the most convenient language for string processing. If you want an easy text processing interface, the best way would be to use perl or python to process the text, then have it interface with a c++ module to do whatever speed dependent task you want done.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Hmm..have a good point there...what about embeding Perl in C++, is that doable without too much hasle?

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I don't know much about perl but with python, it's quite complicated to embed python in c++.

    I do it the other way around. I call c++ modules from within python.

    Try this:
    http://www.swig.org

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    For what amounts to a couple of lines of code, trying to add in say perl is an awful lot of work.

    Only lowercase letters -
    Code:
    if ( isalpha(ch) ) {
      ch = tolower(ch);
      // You're home now, do what you want
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed