开发者

Getting size of files in a directory

I trying to get size of files in a directory by using dirent.h headers. However

stat(ent->d_name, &statbuf)

returns always -1 and I can't get the size properly. Wha开发者_JAVA技巧t could be the reason?


d_name contains the name of the file within that directory. stat wants a name including the directory part, unless it's the current directory.

Make a temporary string containing the full path to the d_name file.

EDIT: Sample

char const * DirName = "/tmp";
....
char * FullName = (char*) malloc(strlen(DirName) + strlen(ent->d_name) + 2); 
strcpy(FullName, DirName);
strcat(FullName, "/");
strcat(FullName, ent->d_name);
stat(FullName, &statbuf);
free(FullName);


I use C++ and I thought the above code would work, but it didn't because it still needed a conversion from void to char*:

char *fullName = (char*) malloc(strlen(path) + strlen(entry->d_name) + 2);

hopefully this can help someone :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜