why cant i edit two files in a program?

This is a discussion on why cant i edit two files in a program? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <fstream.h> #include <string.h> #include <stdlib.h> int main() { ofstream fout("getps.ftp"); fout << "get fomky.123456\n"; fout.close(); ofstream fout("test.bat"); fout ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    why cant i edit two files in a program?

    Code:
    #include <fstream.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {	
    	ofstream fout("getps.ftp");
    	fout << "get fomky.123456\n";
    	fout.close();
    	
                    ofstream fout("test.bat");
    	fout << "edit getps.ftp\n";
    	fout.close();
    
    	system("test.bat");
    
    	return 0;
    }
    there is error called in the line ofstream fout("test.bat"); which said " ofstream fout redefinition"
    does it mean that i cant write data in two different files in the same program?
    what should i do to over come the problem?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    because you gave both the same name

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Code:
    #include <fstream.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {	
    	ofstream fileone("getps.ftp");  // first one 
    	fileone << "get fomky.123456\n";
    	fileone.close();
    	
                    ofstream filetwo("test.bat");
    	filetwo << "edit getps.ftp\n";
    	filetwo.close();
    
    	system("test.bat");
    
    	return 0;
    }
    You can call your 'fout' var whatever you want, in this case I called them fileone and filetwo.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    thank you sooo much~~

    solved. thank you !

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    You can also reuse the same variable just don't try to re-declare it.
    Code:
    #include <fstream.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
      ofstream fout("getps.ftp");
      fout << "get fomky.123456\n";
      fout.close();
    
      fout.open("test.bat");
      fout << "edit getps.ftp\n";
      fout.close();
    
      system("test.bat");
    
      return 0;
    }
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  2. Replies: 5
    Last Post: 02-11-2008, 01:36 AM
  3. Help on renaming files using a C program
    By sangken in forum C Programming
    Replies: 21
    Last Post: 08-03-2006, 06:30 PM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. need help with menu program and header files
    By DFC in forum C++ Programming
    Replies: 12
    Last Post: 12-07-2005, 12:09 PM

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