2011-11-30 40 views
1

我在遇到一個奇怪的問題時,列出了只有目錄或常規文件的子樹中的文件。列出了文件,但在下列程序遍歷它們後文件的大小變爲零。我如何防止這種情況發生?是因爲stat()嗎?stat()後文件變爲零字節

struct list_rec{ 
    int seqno; 
    int path_length; 
    char *file_path; 
}; 
FILE *listoffiles; 
int no_of_files;/*couter to keep track of files visited*/ 
static int list_the_files(const char *fpath,const struct stat *sb, int tflag, struct FTW *ftwbuf) 
{ 
    int fd; 
    struct list_rec t; 
    struct stat buff; 

    if(stat(fpath,&buff) == -1) 
printf("Error reading inode"); 

    /*the file will be listed if it is directory or if it is a regular file and can be opened for writing*/ 
    if(S_ISDIR(buff.st_mode)==0) 
    { 
//printf("\nIT IS NOT A DIRECTORY %s",fpath); 
if(S_ISREG(buff.st_mode)!=0) 
{ 
    //printf("IT IS A REGULAR FILE %s",fpath); 
    fd = open (fpath,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 
    if(fd != -1) 
    { 
     close(fd); 
     no_of_files = no_of_files+1; 
     t.seqno = no_of_files; 
     t.path_length = strlen(fpath)+1; 
     t.file_path = malloc(sizeof(char)*t.path_length); 
     strcpy(t.file_path,fpath); 

     fwrite(&t.seqno,sizeof(int),1,listoffiles); 
     fwrite(&t.path_length,sizeof(int),1,listoffiles); 
     fwrite(t.file_path,sizeof(char)*t.path_length,1,listoffiles); 

     free(t.file_path); 
    } 
    else 
     printf("\n is regular but could not open %s",fpath); 
} 
else 
    printf("\nNot a regular file %s",fpath);  
} 
else 
{ 
    no_of_files = no_of_files+1; 
t.seqno = no_of_files; 
t.path_length = strlen(fpath)+1; 
t.file_path = malloc(sizeof(char)*t.path_length); 
strcpy(t.file_path,fpath); 

fwrite(&t.seqno,sizeof(int),1,listoffiles); 
fwrite(&t.path_length,sizeof(int),1,listoffiles); 
fwrite(t.file_path,sizeof(char)*t.path_length,1,listoffiles); 

free(t.file_path); 
    } 
return 0; 
} 

int main(int argc, char *argv[]) 
{ 
    listoffiles = fopen("/home/juggler/files.txt","w+b"); 
    no_of_files = 0; 
    ftw(argv[1],list_the_files,20); 
    fwrite(&no_of_files,sizeof(int),1,listoffiles);//Writing the total number of records at the end of the file 
    } 

謝謝。幫助將不勝感激

回答

4

你打開的文件與O_WRONLYO_TRUNC,這將截斷文件爲0長度。你可能只想要O_RDONLY

2

不,這是因爲你用O_TRUNC打開文件,該文件將其截斷爲零字節。

如果要檢查文件是否可以打開寫入,使用方法:

if (access(fpath, W_OK) == 0) { 
    /* file can be opened for writing */