Non-Working Code

This is a discussion on Non-Working Code within the C++ Programming forums, part of the General Programming Boards category; Hi Gurus, I'm not able to get this code to work. Can any one help please? Code: #include <fstream> #include ...

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

    Non-Working Code

    Hi Gurus,

    I'm not able to get this code to work. Can any one help please?

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string c,d = "";
    	fstream reads;
    	reads.open("R0_1st.txt",ios ::in);
    	fstream writes;
    	writes.open("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;
    }
    The textfile returns nothing. Does anyone know where I am going wrong?

    Thanks,

    Sheilah

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The textfile returns nothing. Does anyone know where I am going wrong?
    You might want to check to see if the files opened successfully.
    Code:
    	reads.open("R0_1st.txt",ios ::in);
    	if (!reads.is_open())
    	{
    		cout << "Unable to open R0_1st.txt" << endl;
    		return 1;
    	}
    
    	fstream writes;
    	writes.open("new.txt",ios::in|ios::out);
    	if (!writes.is_open())
    	{
    		cout << "Unable to open new.txt" << endl;
    		return 2;
    	}

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And I'm glad someone deleted Cool-August's post.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> And I'm glad someone deleted Cool-August's post.

    Actually, I did that after realizing how stupid it was.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    LOL

    Thanks for the idea. And thanks for being nice about it

    Quote Originally Posted by swoopy
    >The textfile returns nothing. Does anyone know where I am going wrong?
    You might want to check to see if the files opened successfully.
    Code:
    	reads.open("R0_1st.txt",ios ::in);
    	if (!reads.is_open())
    	{
    		cout << "Unable to open R0_1st.txt" << endl;
    		return 1;
    	}
    
    	fstream writes;
    	writes.open("new.txt",ios::in|ios::out);
    	if (!writes.is_open())
    	{
    		cout << "Unable to open new.txt" << endl;
    		return 2;
    	}

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > Does anyone know where I am going wrong?
    Well there's the utterly inept approach of copying someone elses work which springs to mind.
    string search and copy in 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.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Well there's the utterly inept approach of copying someone elses work which springs to mind.
    Amazing, they must be in the same class.

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> http://cboard.cprogramming.com/showp...9&postcount=29

    Sheilah, why did you try to compile script that you already knew didn't work?

  9. #9
    Registered User theFOX's Avatar
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    6
    Hello SheilahT,

    Just make sure you have a '\n' at the end of that "new.txt" if you donnot have one there, the put pointer will be placed at the end of the file so that you will not be able to write at that position.

    Hope that helps.

    Regards.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    >Well there's the utterly inept approach of copying someone elses work which springs to mind.
    Working together.

    Sorry
    Last edited by SheilahT; 09-19-2006 at 04:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 07:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 11:00 AM

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