Stat function segment error

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:

clipboard.png
GDB:

clipboard.png
I hope you guys can give me a hint for this beginner. Thank you

Mar.12,2021

  1. is not necessarily an absolute path, and a relative path is also possible
  2. The error in the
  3. paragraph is not reported by stat (when you are in gdb, you find that stat did not report an error, so don't look down), but printf ("% 15s\ t", ctime (st.st_atime)); and the following sentence. When calling ctime, turn over man to see how ctime is used
  4. .
  5. scanf ("% s", & dirp); don't you think it's unnatural here?
  6. the place where you spell absPath is also wrong
Menu