|
|
在拷贝某个文件夹下的所有所有文件的程序中,出现错误。
在程序中用箭头指出来的地方,就是opendir() 的位置。一运行就提示 printf() 输出的语句 “open directory error!”也就是说打不开制定的文件夹 argv[1]
程序如下:
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <string.h>
- void cpfiles(char *src,char *des,char name[])
- int main(int argc,char *argv[])
- {
- struct stat attrib;
- struct dirent *fname;
- int n;
- DIR *dp;
- if(argc!=3)
- {
- fprintf(stderr,"Usage: %s fromfile tofile\n\a",argv[0]);
- exit(1);
- }
- lstat(argv[1],&attrib);
- if(S_ISDIR(attrib.st_mode))
- {
- for(n=0; ; )
- {
- ——〉 if((dp=opendir(argv[1]))==NULL) 〈——
- fprint("open directory error!\n");
-
- seekdir(dp,n++);
-
- if((fname=readdir(dp))==NULL);
- ..........................这个 for 循环还很长的.遇到合适的条件就会 break 出去..后面的就省略了哦
复制代码
后来又写了一个测试小程序,:
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <string.h>
- int main(int argc,char *argv[])
- {
- DIR *dp;
- if((dp=opendir(argv[1]))==NULL)
- fprint("open directory error!\n");
- return0;
- }
复制代码
运行没错!
这两个程序“./”时设置的路径参数,也就是将要被拷贝的源文件夹路径完全一样,可是上面那个程序opendir 出错,而下面的测试程序却没有!!
不知道是不是在对 argv[1]作的属性操作影响了后面的打开呢?
请大家帮忙看看啦!多谢多谢~~ |
|