c++ - 让 c 程序执行另一个程序的麻烦

标签 c++ windows

我主要感兴趣的是让 C 程序的简单目标

我只是想弄个C程序来做
c:\windows\system32\cmd.exe/k 目录 要么 c:\windows\system32\cmd.exe/k c:\windows\system32\cmd.exe/k 目录

我找到了一个 Windows C 编译器..叫做 lcc-win32

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

这似乎有效。

最佳答案

您打开的是可执行文件,而不是执行它。查看“系统”调用。

关于c++ - 让 c 程序执行另一个程序的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7167116/

相关文章:

c++ - Clang 的 libc++:在 std::chrono::duration 的定义中使用 long long

c++ - 与 C++ 库链接时,如何在 OCaml 编译中抑制 g++ 弃用警告?

c++ - 为什么不能将模板函数作为模板模板参数传递?

windows - Windows 网络共享上的 GIT 中央存储库

c++ - 有什么方法可以阻止 _popen 打开 DOS 窗口吗?

windows - 通过 Windows 命令行启动网站

windows - 使用批处理添加一个exe启动

c++ - 对通过引用返回感到困惑

c++ - 如何在 Visual Studio (2008) 的特定实例中打开源文件

windows - Windows 上的 Git : What do the crlf settings mean?