无法让c程序执行另一个程序

时间:2011-08-23 20:26:12

标签: c++ windows

虽然不是编程,但我很陌生,所以对我很陌生..我主要对于获得C程序的简单目标感兴趣

我只想得到一个C程序来做 c:\ windows \ system32 \ cmd.exe / k目录 要么 c:\ windows \ system32 \ cmd.exe / k c:\ windows \ system32 \ cmd.exe / k dir

我找到了一个名为lcc-win32

的Windows C编译器

这是我正在使用的代码,目前只是为了启动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

我希望在网上找到一些我可以修改的示例代码但是我甚至无法编译任何这样的代码。

- 加入---

我找到了一些示例代码,我想出了一些编程经验。

#include <stdlib.h>

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

这似乎有效

2 个答案:

答案 0 :(得分:2)

您正在打开可执行文件,而不是执行它。查看“系统”电话。

答案 1 :(得分:1)

首先,你需要逃避斜线字符。 "\\"转换为单个反斜杠。

但从它看来,你根本不需要担心这一点。我看到你正在使用系统命令。您根本不需要执行cmd.exe即可使用系统。试试吧

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

    return 0;
}