C++文件操作 判断文件是否存在和文件大小

简介:
在使用C++进行系统开发时,经常用到对文件进行操作的方法,比如判断文件是否存在、获得文件的大小和创建时间等等。下面是我写的一个关于文件操作的类,里面不含有文件读写操作,只含有文件的外围操作。如果读者需要添加文件的读写操作,可以在类里面添加方法,使用文件流操作fstream进行读写。
编译和运行环境是在VC++6.0,File.h如下:
InBlock.gif#ifndef _FILE_H
InBlock.gif#define _FILE_H
InBlock.gif
#include < string>
InBlock.gif
namespace zpp 
InBlock.gif{
InBlock.gif class File {
InBlock.gif   private:
InBlock.gif    std:: string fileName;
InBlock.gif
   public:
InBlock.gif    File( const std:: string& aFileName);
InBlock.gif    ~File();
InBlock.gif
     bool exist();
InBlock.gif     bool isDirectory();
InBlock.gif
     long getFileSize();
InBlock.gif     char getFileDrive();
InBlock.gif    std:: string getCreateTime();
InBlock.gif    std:: string getModifiedTime();
InBlock.gif  };
InBlock.gif}
InBlock.gif
#endif
File.cpp文件如下:
InBlock.gif#include <File.h>
InBlock.gif#include <sys/stat.h>
InBlock.gif#include <time.h>
InBlock.gif
namespace zpp
InBlock.gif{
InBlock.gif  File::File( const std:: string& aFileName):fileName(aFileName) {
InBlock.gif  
InBlock.gif  }
InBlock.gif
  File::~File() {
InBlock.gif  
InBlock.gif  }
InBlock.gif
   bool File::exist() {
InBlock.gif     struct _stat buf;
InBlock.gif     int result;
InBlock.gif    result = _stat(fileName.c_str(), &buf);
InBlock.gif     return (result == 0);
InBlock.gif  }
InBlock.gif
   bool File::isDirectory() {
InBlock.gif     struct _stat buf;
InBlock.gif     if ( _stat(fileName.c_str(), &buf) != 0 ) {     //判断是否存在
InBlock.gif       return  false;
InBlock.gif    }
InBlock.gif     return ( (buf.st_mode & S_IFDIR) !=0 );
InBlock.gif  }
InBlock.gif
   long File::getFileSize() {
InBlock.gif     struct _stat buf;
InBlock.gif     int result;
InBlock.gif    result = _stat(fileName.c_str(), &buf);
InBlock.gif     if ( result == 0 ) {
InBlock.gif       return buf.st_size;
InBlock.gif    }
InBlock.gif     return 0;
InBlock.gif  }
InBlock.gif
   char File::getFileDrive() {
InBlock.gif     struct _stat buf;
InBlock.gif     int result;
InBlock.gif    result = _stat(fileName.c_str(), &buf);
InBlock.gif     if ( result == 0 ) {
InBlock.gif       return ( buf.st_dev + 'A' );
InBlock.gif    }
InBlock.gif     return '0';
InBlock.gif  }
InBlock.gif
  std:: string File::getCreateTime() {
InBlock.gif     struct _stat buf;
InBlock.gif     int result;
InBlock.gif    result = _stat(fileName.c_str(), &buf);
InBlock.gif     if ( result == 0 ) {
InBlock.gif       return std:: string( ctime(&buf.st_ctime) );
InBlock.gif    }
InBlock.gif     return  "0";
InBlock.gif  }
InBlock.gif
  std:: string File::getModifiedTime() {
InBlock.gif     struct _stat buf;
InBlock.gif     int result;
InBlock.gif    result = _stat(fileName.c_str(), &buf);
InBlock.gif     if ( result == 0 ) {
InBlock.gif       return std:: string( ctime(&buf.st_atime) );
InBlock.gif    }
InBlock.gif     return  "0";
InBlock.gif  }
InBlock.gif}


     本文转自panpan3210 51CTO博客,原文链接:http://blog.51cto.com/panpan/102625 ,如需转载请自行联系原作者





相关文章
|
1月前
|
存储 分布式数据库 API
技术好文:VisualC++查看文件被哪个进程占用
技术好文:VisualC++查看文件被哪个进程占用
|
1月前
|
存储 C++
C++文件操作
C++文件操作
13 1
|
1月前
|
C++
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
23 0
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
|
1月前
|
C++ iOS开发 开发者
C++一分钟之-文件输入输出(I/O)操作
【6月更文挑战第24天】C++的文件I/O涉及`ifstream`, `ofstream`和`fstream`类,用于读写操作。常见问题包括未检查文件打开状态、忘记关闭文件、写入模式覆盖文件及字符编码不匹配。避免这些问题的方法有:检查`is_open()`、显式关闭文件或使用RAII、选择适当打开模式(如追加`ios::app`)以及处理字符编码。示例代码展示了读文件和追加写入文件的实践。理解这些要点能帮助编写更健壮的代码。
33 2
|
1月前
|
IDE 开发工具 C++
插件:CLion中使用C/C++ Single File Execution插件编译和运行单个文件
插件:CLion中使用C/C++ Single File Execution插件编译和运行单个文件
30 0
|
1月前
|
Linux C++
Linux C/C++目录和文件的更多操作
Linux C/C++目录和文件的更多操作
|
1月前
|
NoSQL Linux C++
Linux C/C++ gdb调试core文件
Linux C/C++ gdb调试core文件
|
3天前
|
C++
什么是析构函数,它在C++类中起什么作用
什么是析构函数,它在C++类中起什么作用?
20 11
|
3天前
|
C++
能不能说一个C++类的简单示例呀?能解释一下组成部分更好了
能不能说一个C++类的简单示例呀?能解释一下组成部分更好了
26 10