鍍金池/ 問答/C  Linux/ stat函數(shù)段錯(cuò)誤

stat函數(shù)段錯(cuò)誤

環(huán)境centos7 編程語言c
今天在用stat函數(shù)讀取文件夾的狀態(tài)是出錯(cuò),一開始直接傳入文件文件夾名返回都是-1,后來查了一下發(fā)現(xiàn)需要傳入絕對(duì)路徑,于是文件名與路徑名組合起來傳入stat之后就報(bào)段錯(cuò)誤,改了半天都沒發(fā)現(xiàn)怎么回事,gdb跟蹤也沒發(fā)現(xiàn)什么問題
下面是代碼:

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
        DIR *dp;
        struct dirent *ep;
        struct stat st;
        char dirp[50];
        char absPath[100];
        printf("請(qǐng)輸入目錄:\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));  //文件創(chuàng)建時(shí)間 
                                        printf("%15s\t",ctime(st.st_mtime)); //文件上次修改時(shí)間 
                                        printf("%ld\n",st.st_size);
                                }
                        }
                }
                closedir(dp);
        }else
        {
                puts("Couldn't open the directory.\n");
        }
        return 0;
       }

運(yùn)行截圖:

clipboard.png
GDB跟蹤打印的信息:

clipboard.png
希望各位大佬給我這個(gè)初學(xué)者一點(diǎn)提示,謝謝

回答
編輯回答
舊螢火
  1. 不一定是絕對(duì)路徑,相對(duì)路徑也是可以的
  2. 段錯(cuò)誤并不是 stat 報(bào)的(你在 gdb 的時(shí)候,發(fā)現(xiàn) stat 沒有報(bào)錯(cuò)怎么不往下看看),而是 printf("%15s\t",ctime(st.st_atime)); 以及下面那句,在調(diào)用 ctime 的時(shí)候,翻下 man 看看 ctime 是怎么用的
  3. scanf("%s",&dirp); 這里你不覺得很不自然嗎?
  4. 你拼 absPath 那個(gè)地方也是不對(duì)的
2017年1月27日 13:22