开发者

trouble getting c program to execute another program

开发者 https://www.devze.com 2023-03-30 19:26 出处:网络
I\'m mainly interested in the simple goal of getting a C program to I just want to get a C program to do

I'm mainly interested in the simple goal of getting a C program to

I just want to get a C program to do

c:\windows\system32\cmd.exe /k dir or c:\windows\system32\cmd.exe /k c:\windows\system32\cmd.exe /k dir

I found a windows C compiler.. called lcc-win32

Here is the code i'm using, at the moment just aiming at launching 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");
}

But i'm getting a lot of errors cpp: c:\cprogs\hw2.c:1 Could not find include file cpp: c:\cprogs\hw2.c:2 Could not find include file and others

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

I'm hoping to just have found some example code on the net that I can amend but I can't even get any such code to compile.

--added---

I found some example code, I figured out to make the \\ 'cos i've some experience programming.

#include <stdlib.h>

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

and that seems to work.


You're opening the executable file, not executing it. Check out the "system" call.


First of all, you need to escape your slash characters. "\\" translates to a single backslash.

But from what it seems, you don't actually need to worry about that at all. I see that you are using the system command. You don't need to execute cmd.exe at all in order to use system. Try simply

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

    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消