在C系统的功能不是为我工作 [英] system function in c is not working for me

查看:91
本文介绍了在C系统的功能不是为我工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code提取密码保护RAR文件。我使用的系统功能调用RAR命令。如果我在系统命令使用的密码,它的工作原理。但随着试图通过密码作为参数,它没有。例如,如果在这个code,如果我使用密码PWD,它给人的错误PWD不被识别为内部或外部命令,可操作的PROGRAME或批处理文件。
但是,如果我改变code,使其
系统(RARê-ppwd wingen.rar),它的工作原理。
任何人都可以给我解释一下我做的,有什么错?先谢谢了。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
INT主(INT ARGC,字符** argv的)
    {
    焦炭PWORD [20];
    的printf(请输入PWORD:);
    得到(PWORD);
    系统((RAR​​ê-p%s的wingen.rar,PWORD));
    的getchar();
    返回0;
    }


解决方案

系统()函数如的printf)不起作用(。您需要创建完整的字符串,然后调用系统()

 字符命令[100];
sprintf的(指挥,RARê-p%s的wingen.rar,PWORD);
系统(指挥);

您现在使用的是逗号操作,这将导致你的'格式字符串'的code完全被你的程序忽略。你有什么有相当于100%写:

 系统(PWORD);

这是presumably不是你想要的。

I am using this code to extract a password protected rar file. I am using the system function to invoke the rar command. If I use the password in the system command, It works. But as tries to pass the password as parameter, It don't. eg if in this code if I use password pwd, it gives the error "pwd is not recognised as internal or external command,operable programe or batch file." But if I change the code and make it "system("rar e -ppwd wingen.rar")" , It works. Can anybody explain me , what mistake I am doing? Thanks in advance.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
    {
    char pword[20];
    printf("enter the pword : ");
    gets(pword);
    system(("rar e -p%s wingen.rar",pword));
    getchar();
    return 0;
    }

解决方案

The system() function doesn't work like printf(). You need to create the full string, and then call system():

char command[100];
sprintf(command, "rar e -p%s wingen.rar", pword);
system(command);

The code you have right now is using the comma operator, which results in your 'format string' being completely ignored by your program. What you have there is 100% equivalent to writing:

system(pword);

Which is presumably not what you wanted.

这篇关于在C系统的功能不是为我工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆