当前位置: 代码迷 >> C语言 >> 函数调用及system问题
  详细解决方案

函数调用及system问题

热度:402   发布时间:2007-12-03 19:57:05.0
函数调用及system问题
attrib(char *files)
{
system("attrib files +s +h +a");
}
定义了一个attrib的函数,利用dos中attrib命令使文件被隐藏,系统,和归档属性.
然后在main中引用它.
我先初使化了几个文件
*p[2]={
"c:\\1.txt",
"d:\\1.txt"}
然后用for使用attrib函数
for(i=0;i<=1;i++)
{
attrib(p[i]);
}
他会提示找不到文件.不知道怎么回事?
=================================================================================
#include "stdio.h"
#include "windows.h"
char *p[2]={
"c:\\1.txt",
"d:\\1.txt",
};
attrib(char *files)
{


system("attrib files +s +h +a");
}
int main(void)
{
    int i,k;
    for(i=0;i<2;i++)
    {
   
      attrib(p[i]);
    }
}

文件执行的结果显示找不到文件.vc环境下编译

[[italic] 本帖最后由 dousao 于 2007-12-3 19:59 编辑 [/italic]]
搜索更多相关的解决方案: 函数  system  

----------------解决方案--------------------------------------------------------
attrib(char *files)
{
system("attrib files +s +h +a");
}
你觉得你那个参数用到了吗?
----------------解决方案--------------------------------------------------------
我是这么想的.把实参的指针传给files然后在system里面调用files.
是不是哪里出错了?
因该怎么改呢?
----------------解决方案--------------------------------------------------------
system("attrib files +s +h +a");


我也知道这里的files有问题.不知道怎么改.才能达到目的
----------------解决方案--------------------------------------------------------
难道system里面不能用变量?恐怕像
----------------解决方案--------------------------------------------------------
关键是你根本没有用到这个变量啊。引号之内的都是字符串,概念先搞清楚
----------------解决方案--------------------------------------------------------
如果system()接受的是c-string,可以考虑用类似于"attrib "+files+" +s +h +a"的方式来传递实参。
一点想法,尚未测试。
----------------解决方案--------------------------------------------------------
先查看一下c盘或d盘根目录下有没有1.txt这个文件
改了下你的程序,其余的相同
#include "stdio.h"
#include "windows.h"
char *p[2]={
"attrib c:\\1.txt +s +h +a",
"attrib d:\\1.txt +s +h +a"
};
attrib(char *files)
{
system(files);//files只保存一个dos命令
}
----------------解决方案--------------------------------------------------------
  相关解决方案