Environment centos7 programming language c
Today, there is an error reading folders with the stat function. At first, the folder name is returned as-1. Later, it is found that the absolute path is needed. After the combination of the file name and path name is passed into stat, an error is reported. After changing it for a long time, no problem is found in gdb tracking.
the following is the code:
-sharpinclude <sys/types.h>
-sharpinclude <sys/stat.h>
-sharpinclude <dirent.h>
-sharpinclude <unistd.h>
-sharpinclude <string.h>
int main(void)
{
DIR *dp;
struct dirent *ep;
struct stat st;
char dirp[50];
char absPath[100];
printf(":\n");
scanf("%s",&dirp);
dp=opendir(dirp);
printf("filename:\ttype:\tPermission\taccesstime\tlastmodtime\tsize\t\n");
if(dp!=NULL)
{
printf("xxx\n");
while(ep=readdir(dp))
{
if(ep->d_name[0]!=".")
{
printf("xx\n");
if(strcmp(dirp,"/")==0)
{
strcpy(absPath,dirp);
strcat(absPath,ep->d_name);
}else
{
strcpy(absPath,dirp);
strcat(absPath,"/");
strcpy(absPath,ep->d_name);
}
printf("%s\n",absPath);
if(stat(absPath,&st)!=-1)
{
printf("%s\t",ep->d_name);
if((st.st_mode&S_IFMT)==S_IFDIR)
printf("Directory\t");
else if((st.st_mode&S_IFMT)==S_IFBLK)
printf("Block special file\t");
else if((st.st_mode&S_IFMT)==S_IFCHR)
printf("character special file\t");
else if((st.st_mode&S_IFMT)==S_IFREG)
printf("Ordinary file\t");
else if((st.st_mode&S_IFMT)==S_IFIFO)
printf("pipefile file\t");
printf("%o\t",st.st_mode&0x1ff);
printf("%15s\t",ctime(st.st_atime)); //
printf("%15s\t",ctime(st.st_mtime)); //
printf("%ld\n",st.st_size);
}
}
}
closedir(dp);
}else
{
puts("Couldn"t open the directory.\n");
}
return 0;
}
run screenshot:
GDB:
I hope you guys can give me a hint for this beginner. Thank you