当前位置: 技术问答>linux和unix
ubuntu下初学用C语言实现find,好像有字符的问题
来源: 互联网 发布时间:2016-07-15
本文导语: 用netbeans调试以下代码,希望实现find的简单功能 但为何发现file_name=direntp->d_name; file_name指向的字符串会出现“344270213”等的数据?这是什么码? strstr(file_name,search_name)!=NULL为何恒为真呢? #include #include #include #include ...
用netbeans调试以下代码,希望实现find的简单功能
但为何发现file_name=direntp->d_name;
file_name指向的字符串会出现“344270213”等的数据?这是什么码?
strstr(file_name,search_name)!=NULL为何恒为真呢?
#include
#include
#include
#include
#include
#define Max_Length 1000
void du_( int , char *[]);
void du_dir_down(char *);
void du_dir_up();
void du_x(char *,int);
void du_printf();
void find_(int, char *[]);
void find_x(char *,char *);
void find_printf(char *);
char du_dir[Max_Length];
int du_dir_len,du_start,find_num;
int main( int ac , char *av[] )
{
char *cmd;
ac=4;
av[0]="./cmd";
av[1]="-find";
av[2]="arphic";
av[3]="/home";
ac--;
if(ac==0){printf("lack of parametern");return 0;}
cmd=*++av;
if(cmd[0]!='-'){printf("please input the type of commondn");return 0;}
if(cmd[1]=='d'&&cmd[2]=='u')
{
du_(ac,av);
return 1;
}
if(cmd[1]=='f'&&cmd[2]=='i'&&cmd[3]=='n'&&cmd[4]=='d')
{
find_(ac,av);
return 1;
}
printf("commond %s not foundn",cmd);
return 0;
}
void find_(int ac , char *av[])
{
char *file_name;
char *dir;
if(ac==1)
{
printf("please input the name of the file that looking for");
return;
}
ac--;file_name=*++av;
if(ac==1)find_x(".",file_name);
else find_x(*++av,file_name);
}
void find_x(char *dirname,char *search_name)//和du函数共用全局变量
{
int i,len;
DIR *dir_ptr;
char *file_name;
struct dirent *direntp;
find_num=0;
for(len=0;dirname[len]!='';len++);
if(dirname[len-1]=='/')len--;
du_dir_len=len;
for(i=0;id_name;
if(file_name[0]=='.')continue;//跳过隐藏、自身或父文件
if(strstr(file_name,search_name)!=NULL);
{
printf("%s/%sn",du_dir,file_name);
find_num++;
}
du_dir_down(file_name);
find_printf(search_name);
}
closedir(dir_ptr);
}
}
void find_printf(char *search_name)
{
DIR *dir_ptr;
char *file_name;
struct dirent *direntp;
struct stat info;
if((dir_ptr=opendir(du_dir))==NULL)
{ du_dir_up(); return;}
else{
while((direntp=readdir(dir_ptr))!=NULL)
{
file_name=direntp->d_name;
if(file_name[0]=='.')continue;//跳过隐藏、自身或父文件
if(strstr(file_name,search_name)!=NULL);
{
printf("%s/%sn",du_dir,file_name);
find_num++;
}
du_dir_down(file_name);
du_printf();
}
closedir(dir_ptr);
}
du_dir_up();
}
//du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_
void du_( int ac , char *av[] )
{
int mode=0;
char *cmd;
if(ac==1)du_x(".",0);
else
{
cmd=*(av+1);
if(cmd[0]=='-')
{
ac--;
av++;
if(cmd[1]=='s')
{
mode=1;
}
else printf("commond %s not foundn",cmd);
if(ac==1)du_x(".",mode);
}
while(--ac)
du_x(*++av,mode);
}
}
void du_dir_down(char file_name[])
{
int i;
du_dir[du_dir_len]='/';
for(i=0;file_name[i]!='';i++)
du_dir[du_dir_len+i+1]=file_name[i];
du_dir_len=du_dir_len+i+1;
du_dir[du_dir_len]='';
}
void du_dir_up()
{
for(;du_dir[du_dir_len]!='/';du_dir_len--);
du_dir[du_dir_len]='';
}
void du_x(char *dirname,int mode)
{ int i,len;
DIR *dir_ptr;
char *file_name;
struct dirent *direntp;
for(len=0;dirname[len]!='';len++);
if(dirname[len-1]=='/')len--;
du_dir_len=len;
for(i=0;id_name;
if(file_name[0]=='.')continue;//跳过隐藏、自身或父文件
du_dir_down(file_name);
du_printf(mode);
}
closedir(dir_ptr);
}
}
void du_printf(int mode)
{
DIR *dir_ptr;
char *file_name;
struct dirent *direntp;
struct stat info;
if((dir_ptr=opendir(du_dir))==NULL)
{
if(du_startd_name;
if(file_name[0]=='.')continue;//跳过隐藏、自身或父文件
du_dir_down(file_name);
du_printf(mode);
}
closedir(dir_ptr);
}
du_dir_up();
}
但为何发现file_name=direntp->d_name;
file_name指向的字符串会出现“344270213”等的数据?这是什么码?
strstr(file_name,search_name)!=NULL为何恒为真呢?
#include
#include
#include
#include
#include
#define Max_Length 1000
void du_( int , char *[]);
void du_dir_down(char *);
void du_dir_up();
void du_x(char *,int);
void du_printf();
void find_(int, char *[]);
void find_x(char *,char *);
void find_printf(char *);
char du_dir[Max_Length];
int du_dir_len,du_start,find_num;
int main( int ac , char *av[] )
{
char *cmd;
ac=4;
av[0]="./cmd";
av[1]="-find";
av[2]="arphic";
av[3]="/home";
ac--;
if(ac==0){printf("lack of parametern");return 0;}
cmd=*++av;
if(cmd[0]!='-'){printf("please input the type of commondn");return 0;}
if(cmd[1]=='d'&&cmd[2]=='u')
{
du_(ac,av);
return 1;
}
if(cmd[1]=='f'&&cmd[2]=='i'&&cmd[3]=='n'&&cmd[4]=='d')
{
find_(ac,av);
return 1;
}
printf("commond %s not foundn",cmd);
return 0;
}
void find_(int ac , char *av[])
{
char *file_name;
char *dir;
if(ac==1)
{
printf("please input the name of the file that looking for");
return;
}
ac--;file_name=*++av;
if(ac==1)find_x(".",file_name);
else find_x(*++av,file_name);
}
void find_x(char *dirname,char *search_name)//和du函数共用全局变量
{
int i,len;
DIR *dir_ptr;
char *file_name;
struct dirent *direntp;
find_num=0;
for(len=0;dirname[len]!='';len++);
if(dirname[len-1]=='/')len--;
du_dir_len=len;
for(i=0;id_name;
if(file_name[0]=='.')continue;//跳过隐藏、自身或父文件
if(strstr(file_name,search_name)!=NULL);
{
printf("%s/%sn",du_dir,file_name);
find_num++;
}
du_dir_down(file_name);
find_printf(search_name);
}
closedir(dir_ptr);
}
}
void find_printf(char *search_name)
{
DIR *dir_ptr;
char *file_name;
struct dirent *direntp;
struct stat info;
if((dir_ptr=opendir(du_dir))==NULL)
{ du_dir_up(); return;}
else{
while((direntp=readdir(dir_ptr))!=NULL)
{
file_name=direntp->d_name;
if(file_name[0]=='.')continue;//跳过隐藏、自身或父文件
if(strstr(file_name,search_name)!=NULL);
{
printf("%s/%sn",du_dir,file_name);
find_num++;
}
du_dir_down(file_name);
du_printf();
}
closedir(dir_ptr);
}
du_dir_up();
}
//du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_du_
void du_( int ac , char *av[] )
{
int mode=0;
char *cmd;
if(ac==1)du_x(".",0);
else
{
cmd=*(av+1);
if(cmd[0]=='-')
{
ac--;
av++;
if(cmd[1]=='s')
{
mode=1;
}
else printf("commond %s not foundn",cmd);
if(ac==1)du_x(".",mode);
}
while(--ac)
du_x(*++av,mode);
}
}
void du_dir_down(char file_name[])
{
int i;
du_dir[du_dir_len]='/';
for(i=0;file_name[i]!='';i++)
du_dir[du_dir_len+i+1]=file_name[i];
du_dir_len=du_dir_len+i+1;
du_dir[du_dir_len]='';
}
void du_dir_up()
{
for(;du_dir[du_dir_len]!='/';du_dir_len--);
du_dir[du_dir_len]='';
}
void du_x(char *dirname,int mode)
{ int i,len;
DIR *dir_ptr;
char *file_name;
struct dirent *direntp;
for(len=0;dirname[len]!='';len++);
if(dirname[len-1]=='/')len--;
du_dir_len=len;
for(i=0;id_name;
if(file_name[0]=='.')continue;//跳过隐藏、自身或父文件
du_dir_down(file_name);
du_printf(mode);
}
closedir(dir_ptr);
}
}
void du_printf(int mode)
{
DIR *dir_ptr;
char *file_name;
struct dirent *direntp;
struct stat info;
if((dir_ptr=opendir(du_dir))==NULL)
{
if(du_startd_name;
if(file_name[0]=='.')continue;//跳过隐藏、自身或父文件
du_dir_down(file_name);
du_printf(mode);
}
closedir(dir_ptr);
}
du_dir_up();
}
|
if(strstr(file_name,search_name)!=NULL); 行尾多了个分号
{
printf("%s/%sn",du_dir,file_name);
find_num++;
}
if行尾多了个分号,不管strstr返回什么,大括号里面的都要执行的
{
printf("%s/%sn",du_dir,file_name);
find_num++;
}
if行尾多了个分号,不管strstr返回什么,大括号里面的都要执行的