collecting data from text file

Discussion in 'C++' started by nick, Jan 24, 2006.

  1. nick

    nick Guest

    hi all,
    Is there anyone who can help in finishing this problem?
    I’m checking a huge text file for some data, there are lots of lines
    where some of
    these lines lets say start with the word "comp". i need only to check
    these
    lines starting with comp and ignore the others.

    Let's say we have: inputA, inputB.

    ex:
    ...
    ...
    ...
    comp "inputA[4]" ... = ...."p25";
    comp "inputB[3]" ... = ...."f32";
    comp "inputA[5]" ... = ...."f45";
    comp "inputA[1]" ... = ...."g234";
    comp "inputA[3]" ... = ...."p65";
    comp "inputB[0]" ... = ...."p23";
    comp "inputB[1]" ... = ...."g54";

    ...
    ...
    ...
    ...
    Now it should print the numbers/letters between the second quotations
    but in a
    certain order as follows:
    First it has to follow the above pattern i.e: inputA then inputB and
    sort their value (desending) .

    So basically the answer should look like:
    The value of :inputA[4] inputA[3] inputA[2] inputA[1] inputA[0]
    inputB[4]…....inputB[0]

    Here in this case the final output should look like: f45 p25 p65 ..
    g234 .. f32
    ...954 p23

    Any help will be appreciated.
    I couldn't have it to work properly, it prints them randomly ïŒ
     
    nick, Jan 24, 2006
    #1
    1. Advertising

  2. nick wrote:
    > hi all,
    > Is there anyone who can help in finishing this problem?


    "Finishing"? You mean you have it started? Where? I mean, if you want
    us to finish something, we need to see the start, don't you think?

    See FAQ section 5, http://www.parashift.com/c -faq-lite/

    > [.. description redacted ..]
    >
    > Any help will be appreciated.
    > I couldn't have it to work properly, it prints them randomly ïŒ


    Is that a question? I don't know the answer. Could you rephrase it?

    V
     
    Victor Bazarov, Jan 24, 2006
    #2
    1. Advertising

  3. nick

    nick Guest

    thanks for your reply :)
    i apologize for not sending my code, i should have included my code but
    i've been ading and deleting over and over till it became meaning less.
    so i have to start from the begining.


    int main(int argc, char *argv[])
    {
    string line;

    ifstream myfile ("c:\\documents and
    settings\\owner\\Desktop\\myfile.txt");
    if (myfile.is_open())
    {
    while (! myfile.eof ( ) )
    {
    getline (myfile,line);
    cout << line << endl;

    }
    myfile.close();
    }

    else cout << "Unable to open file"<<endl;



    system("PAUSE");
    return 0;
    }
     
    nick, Jan 24, 2006
    #3
  4. nick wrote:
    > thanks for your reply :)
    > i apologize for not sending my code, i should have included my code
    > but i've been ading and deleting over and over till it became meaning
    > less. so i have to start from the begining.


    Is http://www.cplusplus.com/doc/tutorial/files.html where you "started
    from the begginning"? Make sure you copy all the stuff correctly.

    >
    >
    > int main(int argc, char *argv[])
    > {
    > string line;
    >
    > ifstream myfile ("c:\\documents and
    > settings\\owner\\Desktop\\myfile.txt");
    > if (myfile.is_open())
    > {
    > while (! myfile.eof ( ) )
    > {
    > getline (myfile,line);
    > cout << line << endl;
    >
    > }
    > myfile.close();
    > }
    >
    > else cout << "Unable to open file"<<endl;
    >
    >
    >
    > system("PAUSE");
    > return 0;
    > }


    The code you provided doesn't compile. Get it to compile. Add comments
    where you think you're doing whatever you think you're doing. If it does
    not work as intended, explain how its behaviour is different from what
    you expect.

    What do you need to do? You need to do something with the lines that
    start with "comp". How do you check if a line (string) starts with some
    specific text? You can use 'substr' member and equality operator.

    Once you find those particular lines, then what? You need to extract
    some substrings, right? Again, use 'substr'. Which substrings? You need
    to look for the quotation marks, right? Use 'find'...

    And read the FAQ, section 5.

    V
     
    Victor Bazarov, Jan 25, 2006
    #4
  5. nick

    Roger Pate Guest

    On 24 Jan 2006 15:30:28 -0800, nick <> wrote:

    > thanks for your reply :)
    > i apologize for not sending my code, i should have included my code but
    > i've been ading and deleting over and over till it became meaning less.
    > so i have to start from the begining.
    >
    >
    > int main(int argc, char *argv[])
    > {
    > string line;
    >
    > ifstream myfile ("c:\\documents and
    > settings\\owner\\Desktop\\myfile.txt");
    > if (myfile.is_open())
    > {
    > while (! myfile.eof ( ) )
    > {
    > getline (myfile,line);
    > cout << line << endl;
    >
    > }
    > myfile.close();
    > }
    >
    > else cout << "Unable to open file"<<endl;
    >
    >
    >
    > system("PAUSE");
    > return 0;
    > }
    >


    Using !eof() like above is an error. See
    http://www.gnomesane.net/code/doc/noteof/
    Using getline as the loop condition means that if a line cannot be read,
    the condition will fail (because getline returns the stream and the stream
    would be in a not-good state). Checking eof() after the stream has failed
    will tell you whether it failed due to eof; however, the only way getline
    could fail is eof. (An out-of-memory condition with the default allocator
    will throw an exception.)

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

    int main( /*there's no reason to even have parameters if you don't use
    them*/ ) {
    using namespace std;

    ifstream in ("c:/documents and settings/owner/Desktop/myfile.txt");

    if ( !in ) {
    cerr << "Unable to open file\n";
    return 1;
    }

    for ( string line; getline( in, line ); ) {
    cout << line << '\n';
    // do something with line
    }

    // if you want the console to stay open after the program runs,
    // run it from a console (e.g. run cmd and type the program's name)
    return 0;

    // no need for in.close(), ifstream will close itself when it destructs
    }


    --
    Roger Pate <>
     
    Roger Pate, Jan 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy

    Collecting data from website ?

    Andy, Aug 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    452
    Curt_C [MVP]
    Aug 5, 2004
  2. =?Utf-8?B?U2lsZWVzaA==?=

    URgent!!! Collecting data from server side

    =?Utf-8?B?U2lsZWVzaA==?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,391
    bruce barker
    Nov 3, 2004
  3. Michael Mitchell via DotNetMonster.com

    Collecting Data on one form and outputing it on another form

    Michael Mitchell via DotNetMonster.com, Jan 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    472
    Michael Mitchell via DotNetMonster.com
    Jan 23, 2005
  4. Replies:
    5
    Views:
    517
    S. Justin Gengo
    Sep 23, 2005
  5. Antal Rutz
    Replies:
    0
    Views:
    382
    Antal Rutz
    Feb 2, 2006
Loading...

Share This Page