2011-08-23 65 views
1

我很新的C雖然沒有節目,我是如此裸露..我主要感興趣的是得到一個C程序麻煩C程序執行另一個程序

的簡單目標,我只是想要得到一個C程序來做
c:\ windows \ system32 \ cmd.exe/k目錄 或 c:\ windows \ system32 \ cmd.exe /kc:\windows\system32\cmd.exe/k dir

我發現一個Windows的C編譯器..稱爲LCC-win32的

這裏是我使用的代碼,此刻只是愛民處的G推出的cmd.exe

#include <iostream> 
#include <fstream> 
using namespace std; 
int main(){ 
    ifstream inFile; 
    inFile.open("c:\windows\system32\cmd.exe"); 
    if(!inFile){ 

     cout<<"Cannot open file bish."<<endl; 
     system("pause"); 
     return 1; 
     } 

    system("pause"); 
} 

,但我得到了很多錯誤的 CPP:C:\ cprogs \ hw2.c:1找不到包含文件 CPP:C:\ cprogs \ HW2。 C:2找不到包含文件 和其他

Warning c:\cprogs\hw2.c: 1 no type specified. Defaulting to int 
Error c:\cprogs\hw2.c: 1 Syntax error; missing semicolon before `namespace' 
Warning c:\cprogs\hw2.c: 1 no type specified. Defaulting to int 
Error c:\cprogs\hw2.c: 1 Syntax error; missing semicolon before `std' 
Warning c:\cprogs\hw2.c: 1 no type specified. Defaulting to int 
Error c:\cprogs\hw2.c: 3 undeclared identifier 'ifstream' 
Warning c:\cprogs\hw2.c: 3 Statement has no effect 
Error c:\cprogs\hw2.c: 3 Syntax error; missing semicolon before `inFile' 
Error c:\cprogs\hw2.c: 3 undeclared identifier 'inFile' 
Warning c:\cprogs\hw2.c: 3 Statement has no effect 
Error c:\cprogs\hw2.c: 4 left operand of . has incompatible type 'int' 
Error c:\cprogs\hw2.c: 4 found 'int' expected a function 
Warning c:\cprogs\hw2.c: 4 unrecognized character escape sequence '\w' (0x486bd7) 
Warning c:\cprogs\hw2.c: 4 unrecognized character escape sequence '\s' (0x486bde) 
Warning c:\cprogs\hw2.c: 4 unrecognized character escape sequence '\c' (0x486be6) 
Warning c:\cprogs\hw2.c: 4 missing prototype 
Error c:\cprogs\hw2.c: 7 undeclared identifier 'cout' 
Error c:\cprogs\hw2.c: 7 operands of << have illegal types 'int' and 'pointer to char' 
Error c:\cprogs\hw2.c: 7 undeclared identifier 'endl' 
Warning c:\cprogs\hw2.c: 7 Statement has no effect 
Warning c:\cprogs\hw2.c: 8 missing prototype for system 
Warning c:\cprogs\hw2.c: 8 Missing prototype for 'system' 
Warning c:\cprogs\hw2.c: 7 possible usage of endl before definition 
Warning c:\cprogs\hw2.c: 7 possible usage of cout before definition 
Warning c:\cprogs\hw2.c: 12 missing prototype for system 
Warning c:\cprogs\hw2.c: 12 Missing prototype for 'system' 
Warning c:\cprogs\hw2.c: 3 possible usage of inFile before definition 
Warning c:\cprogs\hw2.c: 3 possible usage of ifstream before definition 
Compilation + link time:0.0 sec, Return code: 1 

我希望只是還發現,我可以修改網一些示例代碼,但我甚至不能得到任何這樣的代碼進行編譯。

--added ---

我發現一些示例代碼,我想通了,使\\「COS我一些編程經驗。

#include <stdlib.h> 

int main() 
{ 
    system("c:\\windows\\system32\\cmd2.exe /v:on c:\\windows\\system32\\cmd2.exe /v:on"); 
    return 0; 
} 

而且似乎工作

+1

順便說一下,你正在編寫C++代碼,而不是C. –

+1

很多小錯誤。如果你使用iostream和fstream,你需要一個C++編譯器。同下面的答案一樣,您正在從文件cmd.exe文件中讀取數據。那麼你會,如果你逃避你的反斜線字符。您需要在Windows中將路徑指定爲C:\\ WINDOWS \\ system32 \\ cmd.exe。嘗試一切,看看會發生什麼。 – arunkumar

+0

至少我的(添加的)示例代碼起作用,並且是C! – barlop

回答

2

你打開可執行文件,不執行它。查看「系統」調用。

+0

這可能是非常多的,我發現一些示例代碼搜索系統,並得到了一些我認爲可行的示例代碼。 – barlop

1

首先,您需要轉義您的斜槓字符。 "\\"轉換爲單個反斜槓。

但從看起來,你根本不需要擔心。我看到你正在使用系統命令。您不需要執行cmd.exe就可以使用系統。試一試

int main() 
{ 
    system("pause"); 

    return 0; 
}