您的位置:首页 > 运维架构 > Linux

Linux下获取文件大小,检索目录函数

2015-02-12 19:52 288 查看
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <string>
#include <iostream>

#define PATH "/media/work/test"

int main(int argc, char **argv)
{
DIR *pDir = opendir(PATH);
if (pDir == NULL)
{
perror("opendir failed.\n");
}
struct dirent *pDirent;
std::string picture("test.cpp");
std::string str;
while((pDirent = readdir(pDir)) != NULL)
{
//printf("name: %s\n", pDirent->d_name);
str = pDirent->d_name;
if (str.find(picture) != str.npos)
{
std::cout<<"string name: "<<str<<std::endl;
break;
}
}
struct stat buf;
if (stat(str.c_str(), &buf) == -1)
{
perror("stat failed.\n");
}
std::cout<< "size: "<<buf.st_size<<std::endl;
std::cout<< "st_mtime: "<<ctime(&buf.st_mtime)<<std::endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  path
相关文章推荐