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 ,如需转载请自行联系原作者





相关文章
|
28天前
|
存储 算法 C++
【C++】C++ QT实现Huffman编码器与解码器(源码+课程论文+文件)【独一无二】
【C++】C++ QT实现Huffman编码器与解码器(源码+课程论文+文件)【独一无二】
|
29天前
|
存储 数据挖掘 C语言
【C/C++】C/C++车辆交通违章管理系统(源码+数据文件)【独一无二】
【C/C++】C/C++车辆交通违章管理系统(源码+数据文件)【独一无二】
|
17天前
|
监控 编译器 C++
【代码讲解】【C/C++】获取文件最后修改的时间(系统时间)
【代码讲解】【C/C++】获取文件最后修改的时间(系统时间)
28 0
|
23天前
|
安全 C++ Windows
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
|
25天前
|
C++
C++通过文件指针获取文件大小
C++通过文件指针获取文件大小
22 0
|
25天前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
28 0
|
25天前
|
存储 算法 搜索推荐
【C++】类的默认成员函数
【C++】类的默认成员函数
|
4天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
3天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。
|
3天前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。