Board logo

标题: C语言中的SYSTEM()函数能调用参数吗? [打印本页]

作者: zzhh612     时间: 2007-10-15 15:37    标题: C语言中的SYSTEM()函数能调用参数吗?

我想在编写一个在DOS命令行下执行的类似DOS命令的程序delh.exe,例如:当输入: delh 1.txt 时,使它能直接删除磁盘中的隐藏文件1.txt.
程序如下 :
#include<stdlib>
#include<stdio.h>
int main(int atgc,char *argv[])
{
     system("attrib" argv[1] "-h -s");
     system("del" argv[1]);
     return 0;
}

编译后程序不能达到预想的结果,不知是如何将命令行参数传递给system函数,使它能正确执行.????
作者: MoFeng     时间: 2007-11-4 00:10    标题: 这样行不。

我试了一下这样可以
#include<iostream.h>
#include<stdlib.h>
#include<string>
using namespace std;

#define MAXBUFLEN 100

int main(int argc,char *argv[]){

        string text;
        string cmdstr;

        if(argc>1){
                text = (string)argv[1];
                cmdstr="attrib " + text;
                system(cmdstr.c_str ());

        }
        else{
        cout<<"文件名不给一个?"<<endl;
        }
     return 0;

}
下面是MSDN中的解释

system, _wsystem
Execute a command.

int system( const char *command );

int _wsystem( const wchar_t *command );

Routine Required Header Compatibility
system <process.h> or <stdlib.h> ANSI, Win 95, Win NT
_wsystem <process.h> or <stdlib.h> or <wchar.h> Win NT

Example

/* SYSTEM.C: This program uses
* system to TYPE its source file.
*/

#include <process.h>

void main( void )
{
   system( "type system.c" );
}


Output

/* SYSTEM.C: This program uses
* system to TYPE its source file.
*/
#include <process.h>
void main( void )
{
   system( "type system.c" );
}
作者: maclover815     时间: 2007-12-14 13:19
支持一下