how can we use a batch file in c++?

前端 未结 6 2158
一整个雨季
一整个雨季 2020-12-15 09:36

MY PURPOSE: I want to make a c++ program that could use DOS commands.

OPTION: I can make a batch file and put into it the DOS commands. But I don\'t know how

6条回答
  •  自闭症患者
    2020-12-15 10:01

    //example that makes and then calls a batch file
    
    #include 
    #include 
    #include 
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        ofstream batch;
        batch.open("mybatchfile.bat", ios::out);
    
        batch <<"@echo OFF\n";
        batch <<":START\n";
        batch <<"dir C:\n";
        batch <<"myc++file 2 >nul\n";
        batch <<"goto :eof\n";
    
        batch.close();
    
        if (argc == 2)
        {
            system("mybatchfiles.bat");
            cout <<"Starting Batch File...\n";
        }
    }
    

提交回复
热议问题