我有这个目录树:
xy@xy:/test# find .
.
./2
./2/Asd
./2/Asd/qwe
./2/Asd/qwe/txt.txt
./1
在“test”文件夹中可以有更多的目录,如 4、5、6,但 asd、Asd、ASD、qwe、Qwe、QWE 是不变的。目的是计算所有可能的路径,如下所示:
/test/1/ASD/QWE/txt.txt
/test/2/asd/qwe/txt.txt
/test/2/Asd/Qwe/txt.txt
然后读取文件(txt.txt)
我创建了(复制并粘贴在一起......)以下内容,它正在工作一段时间。我标记了我有问题的行。
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
char *filetosearch="txt.txt";
char *test="/test/";
char *slash="/";
char *directory[3][3] = {{"asd", "Asd", "ASD"}, {"qwe","Qwe","QWE"}};
int dircounter1=0;
int dircounter2=0;
FILE *filetosf;
char **dirpatharr=NULL;
int dirpathcount=0;
char **hdarr=NULL;
int hdarrcount=0;
char **fullpath=NULL;
int count,size;
char *dirname;
DIR *d;
struct dirent *dir;
int main(void)
{
d = opendir(test);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if( dir->d_type==DT_DIR )
{
dirname=dir->d_name;
if((strcmp( dirname, ".." ) == 0) || (strcmp( dirname, "." ) == 0) )
{
}
else
{
hdarr=(char **)realloc(hdarr,(hdarrcount+1)*sizeof(dirname));
hdarr[hdarrcount]=dirname;
hdarrcount++;
}
}
else
{
}
}
}
closedir(d);
for (count=0; count<hdarrcount; count++ )
{
for (dircounter1 = 0; dircounter1 < 3; dircounter1++ )
{
for (dircounter2 = 0; dircounter2 < 3; dircounter2++ )
{
fullpath = malloc ( strlen(test) + strlen(hdarr[count]) + strlen(slash) + strlen(directory[0][dircounter2]) + strlen(slash) + strlen(directory[1][dircounter1]) + strlen(slash) + strlen(filetosearch)+1);
strcpy (fullpath,test);
strcat (fullpath,hdarr[count]);
strcat (fullpath,slash);
strcat (fullpath,directory[0][dircounter2]);
strcat (fullpath,slash);
strcat (fullpath,directory[1][dircounter1]);
strcat (fullpath,slash);
strcat (fullpath,filetosearch);
printf("fullpath:%s\n",fullpath);
// filetosf = fopen(fullpath,"r"); //THIS WILL BE THE PROBLEM!!!!
if (filetosf){
fseek (filetosf, 0L, SEEK_END);
size=ftell(filetosf);
fclose(fullpath);
printf("file exist:%s\n",fullpath);
}
free (fullpath);
}
}
}
}
- - - -剪这里 - - - - - - - - - -
它有这个输出(就像我想要的那样):
fullpath:/test/2/asd/qwe/txt.txt
fullpath:/test/2/Asd/qwe/txt.txt
fullpath:/test/2/ASD/qwe/txt.txt
fullpath:/test/2/asd/Qwe/txt.txt
fullpath:/test/2/Asd/Qwe/txt.txt
fullpath:/test/2/ASD/Qwe/txt.txt
fullpath:/test/2/asd/QWE/txt.txt
fullpath:/test/2/Asd/QWE/txt.txt
fullpath:/test/2/ASD/QWE/txt.txt
fullpath:/test/1/asd/qwe/txt.txt
fullpath:/test/1/Asd/qwe/txt.txt
fullpath:/test/1/ASD/qwe/txt.txt
fullpath:/test/1/asd/Qwe/txt.txt
fullpath:/test/1/Asd/Qwe/txt.txt
fullpath:/test/1/ASD/Qwe/txt.txt
fullpath:/test/1/asd/QWE/txt.txt
fullpath:/test/1/Asd/QWE/txt.txt
fullpath:/test/1/ASD/QWE/txt.txt
但是当我删除备注符号并激活文件打开部分filetosf = fopen(fullpath,"r");
时,我无法读取文件。当我检查输出时,我得到了这个:
fullpath:/test/2/asd/qwe/txt.txt
fullpath:/test//Asd/qwe/txt.txt
fullpath:/test//ASD/qwe/txt.txt
fullpath:/test//asd/Qwe/txt.txt
fullpath:/test//Asd/Qwe/txt.txt
fullpath:/test//ASD/Qwe/txt.txt
fullpath:/test//asd/QWE/txt.txt
fullpath:/test//Asd/QWE/txt.txt
fullpath:/test//ASD/QWE/txt.txt
fullpath:/test//asd/qwe/txt.txt
fullpath:/test//Asd/qwe/txt.txt
fullpath:/test//ASD/qwe/txt.txt
fullpath:/test//asd/Qwe/txt.txt
fullpath:/test//ASD/Qwe/txt.txt
fullpath:/test//asd/QWE/txt.txt
fullpath:/test//Asd/QWE/txt.txt
fullpath:/test//ASD/QWE/txt.txt
如果我在推荐之前打印出这些行,甚至会发生这种情况fopen
。为什么会这样?
我不是一个经验丰富的程序员,所以请尽量简单地解释一下。