extract data from file

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

    #1

    extract data from file


    Hi all
    can any one please tell me what is wrong in this code??
    I'm new to deal with text files and extract data.
    i'm trying to look for data in a text file (3~4 pages) some lines start

    with a word "red" first if find(red) then print the last 5 letters of
    that string and if red is not found at the begining of the string then
    do nothing and
    go to another line.
    how can I also do this using find( )??
    thanks


    #include <iostream>
    #include <fstream>
    #include <string>

    int main( )
    {
    ifstream textfile ("textfile.txt" );

    string text;
    string::size_ty pe posn;

    if (textfile.is_op en())
    {
    while (getline(textfi le, text))
    {
    posn = text.find("red" );
    if (line.search(re d) != std::string:no_ pos)
    //if (posn == string::npos)
    {
    continue;
    }


    std::string data = text.substr(5);
    cout<<" " << data << endl;
    }
    }


    else cout << "can't open file" << endl;


    system("PAUSE") ;
    return EXIT_SUCCESS;
    }

  • Victor Bazarov

    #2
    Re: extract data from file

    nick wrote:[color=blue]
    > can any one please tell me what is wrong in this code??[/color]

    Does it compile? If no, what errors do you get? If it does compile, does
    it work as intended? Why should we tell you when you probably already
    know what is wrong? Now, once you tell us what is not the way you want it
    to be, we could try to help you to make it the way it should be. But
    without knowing the symptoms, how can anybody tell?
    [color=blue]
    > I'm new to deal with text files and extract data.
    > i'm trying to look for data in a text file (3~4 pages)[/color]

    Perhaps you should start with a simple set of test data. Like a file that
    only contains three lines...

    [color=blue]
    > some lines start
    >
    > with a word "red" first if find(red) then print the last 5 letters of
    > that string and if red is not found at the begining of the string then
    > do nothing and
    > go to another line.
    > how can I also do this using find( )??[/color]

    Aren't you already using 'find'?
    [color=blue]
    > thanks
    >
    >
    > #include <iostream>
    > #include <fstream>
    > #include <string>[/color]

    You need

    using namespace std;

    here, probably.
    [color=blue]
    >
    > int main( )
    > {
    > ifstream textfile ("textfile.txt" );
    >
    > string text;
    > string::size_ty pe posn;
    >
    > if (textfile.is_op en())
    > {
    > while (getline(textfi le, text))
    > {
    > posn = text.find("red" );[/color]

    I think you need to (a) skip whitespace in 'text', then (b) extract the
    first three chars of 'text' using the 'substr' member and compare the
    extracted string with "red".
    [color=blue]
    > if (line.search(re d) != std::string:no_ pos)[/color]

    What's "search"? What's "line"? Don't you mean to compare what you
    just obtained from 'find'?

    Even if you do compare the right things, it doesn't necessarily mean your
    "word" is found at all. If I have a word "referred" or "predicate"
    somewhere in a line, your 'find' will give you the position of it, but
    it won't satisfy the condition you stated (line beginning with the word
    "red").
    [color=blue]
    > //if (posn == string::npos)
    > {
    > continue;
    > }
    >
    >
    > std::string data = text.substr(5);
    > cout<<" " << data << endl;
    > }
    > }
    >
    >
    > else cout << "can't open file" << endl;
    >
    >
    > system("PAUSE") ;
    > return EXIT_SUCCESS;
    > }
    >[/color]

    V

    Comment

    • int2str@gmail.com

      #3
      Re: extract data from file


      nick wrote:[color=blue]
      > Hi all
      > can any one please tell me what is wrong in this code??[/color]

      It doesn't compile for once.
      Please post compilable code if you want us to help you with a language
      related problem.
      [color=blue]
      > I'm new to deal with text files and extract data.
      > i'm trying to look for data in a text file (3~4 pages) some lines start
      > with a word "red" first if find(red) then print the last 5 letters of
      > that string and if red is not found at the begining of the string then
      > do nothing and
      > go to another line.
      > how can I also do this using find( )??[/color]

      Assuming this is no homework....

      If the lines START with the word 'red' and there's no leading
      whitespace etc. You don't need to use find. Indeed find() may yield
      incorrect results if you have lines like "greenish red 12345".
      [color=blue]
      > thanks
      >
      >
      > #include <iostream>
      > #include <fstream>
      > #include <string>
      >[/color]

      using namespace std;

      or

      using std::string; // etc.
      [color=blue]
      > int main( )
      > {
      > ifstream textfile ("textfile.txt" );
      >
      > string text;
      > string::size_ty pe posn;[/color]

      posn can be declared closer to where it's used.
      [color=blue]
      >
      > if (textfile.is_op en())
      > {
      > while (getline(textfi le, text))
      > {
      > posn = text.find("red" );
      > if (line.search(re d) != std::string:no_ pos)[/color]

      What's "line", what's "search" and what is "no_pos" ???
      Also your condition is the inverse of the one in the comment.
      [color=blue]
      > //if (posn == string::npos)
      > {
      > continue;
      > }
      >
      >
      > std::string data = text.substr(5);[/color]

      This will give you the string from position 5 in text onwards. As in,
      given text is "Hello World", data will be " World". This is not what
      you want.
      [color=blue]
      > cout<<" " << data << endl;
      > }
      > }
      >
      >
      > else cout << "can't open file" << endl;[/color]

      If you used curly braces for the if(), use it for the else, too.
      Also fix your indenting etc.
      [color=blue]
      > system("PAUSE") ;[/color]

      Not portable.
      [color=blue]
      > return EXIT_SUCCESS;[/color]

      Unecessary.
      [color=blue]
      > }[/color]

      Cheers,
      Andre

      Comment

      • nick

        #4
        Re: extract data from file

        so what is the best way to check the first word in a line?
        if match then do some thing and if not then skip to next line.

        Comment

        • int2str@gmail.com

          #5
          Re: extract data from file


          nick wrote:[color=blue]
          > so what is the best way to check the first word in a line?
          > if match then do some thing and if not then skip to next line.[/color]

          Please quote what you are replying to.

          It seems what you have posted originally is "meta code". Why not pick
          up a book, or research the actual functions and classes online. Learn
          about std::string, how to use substr(), == etc.

          Post back what you come up with.

          Cheers,
          Andre

          Comment

          • nick

            #6
            Re: extract data from file

            Please quote what you are replying to

            If the lines START with the word 'red' and there's no leading
            whitespace etc. You don't need to use find. Indeed find() may yield
            incorrect results if you have lines like "greenish red 12345".

            here the word red only exists at the begining of the certain lines.

            in general: what is the best way to check the first word in a line?
            if match then do some thing and if not then skip to next line.

            Comment

            • red floyd

              #7
              Re: extract data from file

              nick wrote:[color=blue]
              > Please quote what you are replying to
              >
              > If the lines START with the word 'red' and there's no leading
              > whitespace etc. You don't need to use find. Indeed find() may yield
              > incorrect results if you have lines like "greenish red 12345".
              >
              > here the word red only exists at the begining of the certain lines.
              >
              > in general: what is the best way to check the first word in a line?
              > if match then do some thing and if not then skip to next line.
              >[/color]

              Actually, you had a reasonable shell: you're using istream, you're using
              std::getline() properly, your not misusing eof().

              Hints:
              look at std::string::fi nd_first_not_of () to find the first non-whitespace.
              Then look at substr()

              Comment

              • red floyd

                #8
                Re: extract data from file

                nick wrote:[color=blue]
                > Please quote what you are replying to
                >
                > If the lines START with the word 'red' and there's no leading
                > whitespace etc. You don't need to use find. Indeed find() may yield
                > incorrect results if you have lines like "greenish red 12345".
                >
                > here the word red only exists at the begining of the certain lines.
                >
                > in general: what is the best way to check the first word in a line?
                > if match then do some thing and if not then skip to next line.
                >[/color]

                Alternatively, look at std::istringstr eam, for parsing your line of input.

                Comment

                • Default User

                  #9
                  Re: extract data from file

                  nick wrote:
                  [color=blue]
                  > Please quote what you are replying to[/color]

                  See below.



                  Brian

                  --
                  Please quote enough of the previous message for context. To do so from
                  Google, click "show options" and use the Reply shown in the expanded
                  header.

                  Comment

                  Working...