首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让c程序执行其他程序时出现问题

让c程序执行其他程序时出现问题
EN

Stack Overflow用户
提问于 2011-08-24 04:26:12
回答 2查看 382关注 0票数 1

我主要感兴趣的是一个简单的目标,即让一个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

代码语言:javascript
复制
#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找不到包含文件和其他文件

代码语言:javascript
复制
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

我希望在网上找到一些我可以修改的示例代码,但我甚至不能得到任何这样的代码来编译。

--添加

我找到了一些示例代码,因为我有一些编程经验,所以我想要做这些。

代码语言:javascript
复制
#include <stdlib.h>

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

这似乎很管用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-24 04:28:26

您正在打开可执行文件,而不是执行它。查看"system“调用。

票数 2
EN

Stack Overflow用户

发布于 2011-08-24 04:49:58

首先,你需要转义你的斜杠字符。"\\"转换为单个反斜杠。

但从表面上看,您实际上根本不需要担心这一点。我看到您正在使用系统命令。要使用系统,根本不需要执行cmd.exe。简单地试一下

代码语言:javascript
复制
int main()
{
    system("pause");

    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7167116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档