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; >But now i'm confused about that (-1) part. Don't worry about it, it's irrelevant to the task at hand. However, ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But now i'm confused about that (-1) part.
    Don't worry about it, it's irrelevant to the task at hand. However, if you're interested, string::size_type is an unsigned type, meaning that it cannot have a negative value (as Mario said). However, if you assign a negative value to an unsigned type, the value wraps around to a large positive value. So if you say:
    Code:
    unsigned int x = -1;
    You're actually setting x's value to the largest positive value that an unsigned integer can hold.
    My best code is written with the delete key.

  2. #17
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Say using void main() is wrong, why does such a large company like MS use it?

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Say using void main() is wrong, why does such a large company like MS use it?
    Because they're arrogant enough to believe that they can rewrite the standard without ISO's permission.
    My best code is written with the delete key.

  4. #19
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Perhaps because MS designed their operating systems to work where main is declared as void, and they don't give a damn about standards?
    "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

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Cool-August
    Say using void main() is wrong, why does such a large company like MS use it?
    They don't believe that Windows qualifies as a "hosted environment"?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> Because they're arrogant enough to believe that they can rewrite the standard without ISO's permission.

    I hate to say it, but, who ever gave the ISO the right over c++?

    >> I'm sorry if it seems that way. We're just on a quest to avoid undefined behavior and promote correct C++.

    Wouldn't you if you where directly told "void is just plain lazy".?
    And you can usualy tell when someone takes offence to somthing someone else has said.

    I will be honest, the only reason I make it int rather than void is because it's one of dev-c++'s requierments. Although, it's a habit now, so I use int even in visual-c++.

    EDIT:

    I also noticed that you seem to think that I am supporting void main().
    I'm not.
    I just don't bash or support somthing untill I have a good reason.
    Last edited by Queatrix; 09-17-2006 at 04:31 PM.

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Your program itself is expected to return a value. Would you expect this to do something meaningful?
    Code:
    #include <stdio.h>
    
    void foo(void) {}
    
    int main()
    {
       printf("return value = %d\n", foo());
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I hate to say it, but, who ever gave the ISO the right over c++?
    ANSI, via the C community. In other words, we did.

    >Wouldn't you if you where directly told "void is just plain lazy".?
    Wouldn't I what? Your sentence doesn't constitute a complete thought.

    >I will be honest, the only reason I make it int rather than void is because it's one of dev-c++'s requierments.
    And you never wondered why it was a requirement in Dev-C++?

    >I also noticed that you seem to think that I am supporting void main().
    Not really, I'm simply answering your questions in an objective manner.

    >I just don't bash or support somthing untill I have a good reason.
    The ISO standard defines C++ for the entire world. It's accepted as the one and final authority when it comes to language questions. It also says that void main is wrong. Is that not a good enough reason?
    My best code is written with the delete key.

  9. #24
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> The ISO standard defines C++ for the entire world.
    >> It's accepted as the one and final authority when
    >> it comes to language questions. It also says that
    >> void main is wrong. Is that not a good enough reason?

    Sure it is, but untill now, I didn't have that reason.

    >Wouldn't you if you where directly told "void is just plain lazy".?
    Wouldn't I what? Your sentence doesn't constitute a complete thought.

    I'm sorry, I read it wrong:
    ME: They take it as an offence and as lazyness.
    YOU: I'm sorry if it seems that way.
    ME: Wouldn't you if you where directly told "void is just plain lazy".?
    I read it as this:
    ME: They take it as an offence and as lazyness.
    YOU: I'm sorry you see it that way.
    ME: Wouldn't you if you where directly told "void is just plain lazy".?

  10. #25
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Sure it is, but untill now, I didn't have that reason.
    Now you do. Learning is what this forum is all about, right?
    My best code is written with the delete key.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Sorry one more question i didn't wanna create another thread for it
    it's small.
    Now i found the string i was looking for in a txt file
    I want to replace this string with another string.
    I've tried but i couldn't do it.
    can someone help me?
    Thanks

  12. #27
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > They don't believe that Windows qualifies as a "hosted environment"?
    Heh - the flipside of the symbiotic relationship is a parasitic environment

    > Say using void main() is wrong, why does such a large company like MS use it?
    Probably because it hails from when they (and Borland) implemented DOS compilers, and DOS was so utterly useless at doing anything meaningful with the status returned that nobody really bothered. But like other poisonous meme viruses, it manages to maintain a fan-base which makes it a lot harder to kill off.

    Of course, if you want to carry on down the road of "what my compiler will let me get away with", don't expect a lot of sympathy when your code is lying in intensive care due to all your use of undefined behavour.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Shady
    I want to replace this string with another string.
    Replacing something in the middle of the file is a doomed endeavor in at least 90% of the cases - for example in yours.

    You need to read the complete file, change what you want to change in memory, and then write the complete file back.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Here is my code
    it compiles
    but nothing is replaced !!! nothing is written in the file !!! nothing happens to "new.txt"
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string c,d = "";
    	fstream reads("R0_1st.txt",ios::in);
    	fstream writes("new.txt",ios::in|ios::out);
    	reads.unsetf(ios::skipws);
    	
    	while ( getline ( reads, c ) ) {
    		cout<<c<<endl;
    		if ( c.find ( "Adiabatic Efficiency" ) != string::npos )
    			
    		{	
    			cout<<">>>>>>>>>>>>>>>"<<endl; //Just to make sure it got here
    			while( getline (writes,d) )
    			{
    				if ( d.find ("replace here") != string::npos )
    				{
    					cout<<"<<<<<<<<<<<<<<<"<<endl; //Just to make sure it got here
    					writes<<c;
    				}
    			}
    		}
    	}
    
    	return 0;
    }

  15. #30
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Why don't you just do read from one file, write to another file and stop monkeying around trying to do the near impossible of writing updates to the middle of a file
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 3 FirstFirst 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, 10:08 AM
  2. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. string search question...
    By flightsimdude in forum C Programming
    Replies: 11
    Last Post: 09-24-2003, 09:11 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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