在从当前路径遍历并搜索文件时,我在定位文件时遇到了问题。
我怀疑这path
是由于chdir
在下一个代码中的原因:
相关的文件夹是:
- 12,13,14 , 12 是父目录
- 13 内 12 , 14 内 13
- david1.txt 内 14
- get_current_dir_name()是一个 Linux 函数,不是我的
我从运行可执行文件Desktop
,像这样(这里是printf-s
为了尝试定位问题):
// doesn't work
a@ubuntu:~/Desktop$ ./exer4 12 david1.txt
Current path is :12
Current path is :/home/a/Desktop/12/13
Current path is :/home/a/Desktop/12/13/14
//////////////////////////////////////////////////
// doesn't work
a@ubuntu:~/Desktop$ ./exer4 12/13 david1.txt
Current path is :12/13
Current path is :/home/a/Desktop/12/13/14
////////////////////////////////////////////////
// doesn't work
a@ubuntu:~/Desktop$ ./exer4 12/13/14 david1.txt
Current path is :12/13/14
如果我删除这些行:
chdir(path); // the problem is probably here
strcpy(path, get_current_dir_name()); // and here
然后每个只涉及一个库名称的查询都有效,例如:
// works :
a@ubuntu:~/Desktop$ ./exer4 12 david1.txt
Current path is :12
Current path is :12/13
File found!
12/13/14/david1.txt-rw-rw-r-- 1 1000 1000 0 2012-06-17 08:10
Current path is :12/13/14
////////////////////////////////////////////////////////////
// works
a@ubuntu:~/Desktop$ ./exer4 12/13 david1.txt
Current path is :12/13
File found!
12/13/14/david1.txt-rw-rw-r-- 1 1000 1000 0 2012-06-17 08:10
Current path is :12/13/14
////////////////////////////////////////////////////////////
// Doesn't work
a@ubuntu:~/Desktop$ ./exer4 12/13/14 david1.txt
Current path is :12/13/14
知道我在这里做错了什么吗?我尝试过无数次更改,但没有任何修复。谢谢