opening a batch file from c++

This is a discussion on opening a batch file from c++ within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering how to open a bat file at the end of a program. we'll say the file ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262

    opening a batch file from c++

    Hi, I was wondering how to open a bat file at the end of a program. we'll say the file was test.bat, and it was in c:\ ... any help would be greatly appreciated!!!!!!

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by twomers
    Hi, I was wondering how to open a bat file at the end of a program. we'll say the file was test.bat, and it was in c:\ ... any help would be greatly appreciated!!!!!!
    What means open ?
    if you mean run
    Code:
    system("c:\\test.bat");
    Kurt

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    that's perfect Kurt!!! thanks a mil!!!!!!!!!!!!!!!

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It depends on what you want to do.
    Code:
    #include <iostream> //cout
    #include <fstream> //ifstream, ofstream
    #include <string>
    using namespace std;
    
    
    int main()
    {
    	//read from file:
    	ifstream inFile("C:\\TestData\\test.bat");
    	if(!inFile)
    	{
    		cout<<"couldn't open file"<<endl;
    		return 1;
    	}
    	
    	string str;
    	inFile>>str;
    	cout<<str<<endl;
    	
    	inFile.close();
    
    	//append text to the end of the file:
    	ofstream outFile("C:\\TestData\\test.bat", ios::app);
    	
    	outFile<<endl<<"some text";
    
    
    	return 0;
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    For system() you need to include <cstdlib>.

    If you want to run it in a seperate DOS window, you can usually use
    Code:
    system("start test.bat");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. batch file introduction in multiprocesses program
    By c_geek in forum C Programming
    Replies: 0
    Last Post: 03-27-2008, 01:52 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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