string search and copy in a file !

This is a discussion on string search and copy in a file ! within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a text file which contains this... Mass Flow Rate = 1.06984 Total Press Ratio = 1.05424 Adiabatic ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    Exclamation string search and copy in a file !

    Hello,
    I have a text file which contains this...
    Mass Flow Rate = 1.06984
    Total Press Ratio = 1.05424
    Adiabatic Efficiency = 0.730169
    Exit Flow Coeff Cr2/U2rms = 0.438395
    Ps2/Pa = 0.990686

    i want to seach for this "Adiabatic Efficiency = 0.730169" and print it.
    this is my code
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    void main()
    {
    	string c;
    	ifstream reads("R0_1st.txt");
    	reads.seekg(ios::beg);
    	reads.unsetf(ios::skipws);
    	while(!reads.eof())
    	{
    	getline (reads,c);
    	cout<<c<<endl;
    	}
    }
    this code prints all the file
    i just want to seach for the string before i print
    Thanks
    Last edited by Shady; 09-17-2006 at 01:54 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main()
    int main(). You have no excuses for not doing it.

    >reads.seekg(ios::beg);
    Why do you think this is needed?

    >while(!reads.eof())
    This is the wrong way to read from a stream. The eofbit is only set after you try and fail to read, so you'll end up processing the last successfully read record twice.

    >i just want to seach for the string before i print
    Well, you're already using std::strings, so it's pretty straightforward:
    Code:
    while ( getline ( reads, c ) ) {
      if ( c.find ( "Adiabatic Efficiency" ) != string::npos )
        cout<< c <<'\n';
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    you can consider me a noob

    why would i need "int main()" and not "void main()" for me it's just a matter of not writing return 0; at the end !! i never knew it means something :S

    and if i'm reading the file the wrong way
    what is the right way ?!

    thank you for replying that was very useful

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> int main(). You have no excuses for not doing it.

    I don't know why so many "higher up" programmers HATE that.
    They take it as an offence and as lazyness.
    I just don't get it.

    So you might say it's there for other programs to get it's return code, so, many just use return 0; everywhere, wether error or no.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why would i need "int main()" and not "void main()"
    Because int is right and void is wrong. Eventually you'll be directed to the C++ standard, where these things are documented, but for now just take our word for it.

    >for me it's just a matter of not writing return 0; at the end !!
    Remember that I said there are no excuses. C++ automatically returns 0 even if you omit the return statement. This is a perfectly legal C++ program that returns 0:
    Code:
    int main()
    {
    }
    >and if i'm reading the file the wrong way
    >what is the right way ?!
    I showed you. You use the return value of getline. It returns the stream, which when used in a conditional statement tells you whether the stream is in a failure state or not. The failure state includes end-of-file, so all you need to do is:
    Code:
    while ( getline ( my_stream, my_string ) ) {
      // Work with a line
    }
    You can do the same thing with basically every input method. Another common one is:
    Code:
    while ( my_stream>> my_var ) {
      // Use the variable
    }
    >I don't know why so many "higher up" programmers HATE that.
    Because it's wrong. The C++ standard explicitly states that main returns an int. If an implementation doesn't provide for void main then the entire program is has undefined behavior. And undefined behavior is the worst possible thing you can have in C++.

    >They take it as an offence and as lazyness.
    I'm sorry if it seems that way. We're just on a quest to avoid undefined behavior and promote correct C++.
    My best code is written with the delete key.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    First hit on google: http://users.aber.ac.uk/auj/voidmain.shtml

    And return 0 is implicit in main as defined by the standard (so you can omit a return statement in main)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    I didn't know what string::npos was there for
    so i searched and this is what i found
    An unsigned integral value initialized to –1 that indicates either "not found" or "all remaining characters" when a search function fails.
    is it right ?
    it sounds reasonable.
    and if so what does it mean by " all remaining characters "?

    Thanks
    Last edited by Shady; 09-17-2006 at 03:21 PM.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,578
    It's not correct that definition. First there is no guarantee that the value of string::npos is -1. But also how can an unsigned integral possibly be -1? It can't.

    npos is defined by the standard as simply being greater than any possible index. That is, it will hold a value that is greater than the last index of the largest possible string. npos is what is returned by several of the string find functions when there is no match.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    how come it's not right?
    i found it on msdn library ! :s
    http://msdn.microsoft.com/library/de...ng_members.asp
    sorry for askin too much but can you explain to me what does it do in Prelude's code?
    Last edited by Shady; 09-17-2006 at 03:34 PM.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,578
    They clearly made 2 mistakes.

    First, if it is unsigned, it can't be negative. Second the typedef defines npos as being of type std::size_type. if std::size_type is unsigned (as they say it is) then it can't be negative.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    may be -1 means null ?

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,578
    No, it doesn't. -1 means -1
    Null is on some contexts intepreted as 0.

    If Microsoft implementation decides to treat npos as being -1, it's their business. Although it will not be according to what the standard defines. But they can't say it's an unsigned integral. It would have to be a signed integral.

    As for the second part of your previous question (sorry missed it before), Prelude is testing against npos to see if the string was found. The find function either returns the index at which the string was found, or that special value mapped to string::npos that means it was not found.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >First, if it is unsigned, it can't be negative.
    But MSDN doesn't say that it's negative, only that it's initialized with -1. That's different.

    >sorry for askin too much but can you explain to me what does it do in Prelude's code?
    The find member function returns npos if the string wasn't found.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Thank you so much guys.
    But now i'm confused about that (-1) part.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,578
    > But now i'm confused about that (-1) part.

    Well, prelude just opened my eyes.

    What happens is that integrals wrap around when they reach their limits. For instance, if an int variable had as minimum value 0 and maximum possible value 100, when you give it a value out of bounds, it will wrap. So, -1 becomes 100, -2 becomes 99. Conversely, 101 becomes 0, 102 becomes 1,...

    So in fact, I'm wrong and the documentation found in MSDN is right (albeit perhaps confusing to the distracted like me). By initializing to -1, they are in fact giving it the maximum possible value.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 11:08 AM
  2. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. string search question...
    By flightsimdude in forum C Programming
    Replies: 11
    Last Post: 09-24-2003, 10:11 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21