parse tilde delimited file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • monte

    parse tilde delimited file

    Hello, I need to parse a tilde delimited file and output it to a
    tabbed delimited file. Example file example.txt

    data1~data2~dat a3~data4
    data5~data6~dat a7~data8

    I need to extract data2, data4, data6 and data8 from the above file
    and output it to a file delimited by tabs:

    data2 data4 data6 data8

    I know how to do the ouput but the parsing I'm not sure about. Thanks
    for your help. Monte.
  • John Harrison

    #2
    Re: parse tilde delimited file


    "monte" <olveram7@yahoo .com> wrote in message
    news:ac124f78.0 409210446.73b4e 811@posting.goo gle.com...[color=blue]
    > Hello, I need to parse a tilde delimited file and output it to a
    > tabbed delimited file. Example file example.txt
    >
    > data1~data2~dat a3~data4
    > data5~data6~dat a7~data8
    >
    > I need to extract data2, data4, data6 and data8 from the above file
    > and output it to a file delimited by tabs:
    >
    > data2 data4 data6 data8
    >
    > I know how to do the ouput but the parsing I'm not sure about. Thanks
    > for your help. Monte.[/color]

    Actually you have two sets of delimiters, end of line and ~. So you need two
    nested loops, the outer reading lines and the inner reading ~ delimited
    text.

    How about this (untested)

    std::string line;
    while (getline(file, line))
    {
    std::istringstr eam buffer(line);
    std::string data;
    while (getine(buffer, data, '~'))
    {
    // do something with data
    }
    }

    john


    Comment

    • Dietmar Kuehl

      #3
      Re: parse tilde delimited file

      > Actually you have two sets of delimiters, end of line and ~. So you
      need two[color=blue]
      > nested loops, the outer reading lines and the inner reading ~[/color]
      delimited[color=blue]
      > text.[/color]

      If all what needs to be done is replacing the tilde characters by tab
      characters, I would just do that:

      | #include <fstream>
      | #include <algorithm>
      | #include <iterator>
      | int main() {
      | std::ifstream in("in.file");
      | std::ofstream out("out.file") ;
      | std::replace_co py(std::istream buf_iterator<ch ar>(in),
      | std::istreambuf _iterator<char> (),
      | std::ostreambuf _iterator<char> (out),
      | '~', '\t');
      | }

      If speed does matter, you will need a good standard C++ library
      implementation for this or you might indeed want to read and
      process individual lines. However, 'std::getline() 'ing a line and
      'std::replace() 'ing the tilde characters should do the trick.
      --
      <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
      <http://www.contendix.c om> - Software Development & Consulting

      Comment

      • Mike Smith

        #4
        Re: parse tilde delimited file

        monte wrote:
        [color=blue]
        > Hello, I need to parse a tilde delimited file and output it to a
        > tabbed delimited file. Example file example.txt[/color]

        Oh, that's right - it's September. How could I have forgotten?

        --
        Mike Smith

        Comment

        Working...