C++/C直接可用的文件操作(待完善)

#include "FileOperator.h"
#include <direct.h>
#include <io.h>
#include <string.h>
#include <sys/stat.h>
#include <iostream>
#include <Windows.h>


FileOperator::FileOperator()
{

}

FileOperator::~FileOperator()
{

}

bool FileOperator::ALIsFile(char* pPath)
{
	struct stat s;

	if (pPath == nullptr)
		return false;
	if (stat(pPath, &s) == 0)
	{
		if (s.st_mode & S_IFDIR)
		{
			return false;
		}
		else if (s.st_mode & S_IFREG)
		{
			return true;
		}
	}
	return false;
}

bool FileOperator::ALDeleteFile(char* pPath)
{
	if (ALIsFile(pPath))
	{
		if (remove(pPath) == 0)
		{
			return true;
		}
	}
	return false;
}

bool FileOperator::ALIsDir(char* pPath)
{
	if (pPath == nullptr)
		return false;
	struct stat s;

	if (pPath == nullptr)
		return false;
	if (stat(pPath, &s) == 0)
	{
		if (s.st_mode & S_IFDIR)
		{
			return true;
		}
		else if (s.st_mode & S_IFREG)
		{
			return false;
		}
	}

	return false;
}

bool FileOperator::ALDeleteDir(char* pPath)
{
    if (ALIsDir(pPath))
    {
        std::string strDir = pPath;
        if (strDir.at(strDir.length() - 1) != '\\')
            strDir += '\\';
        WIN32_FIND_DATAA wfd;
        HANDLE hFind = FindFirstFileA((strDir + "*.*").c_str(), &wfd);
        if (hFind == INVALID_HANDLE_VALUE)
            return false;
        do
        {
            if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if (_stricmp(wfd.cFileName, ".") != 0 && _stricmp(wfd.cFileName, "..") != 0)
                {
                    ALDeleteDir((char*)(strDir + wfd.cFileName).c_str());
                }
            }
            else
            {
                DeleteFileA((strDir + wfd.cFileName).c_str());
            }
        } while (FindNextFileA(hFind, &wfd));
        FindClose(hFind);
        RemoveDirectoryA(pPath);
        return true;
    }
	return false;
}

std::string FileOperator::ALGetFileNameWithOutExt(char* pPath)
{
    std::string strFileName = ALGetFileName(pPath);
    if (strFileName.size() != 0)
    {
        std::size_t found = strFileName.find_last_of(".");
        strFileName = strFileName.substr(found + 1);
    }
    return strFileName;
}


std::string FileOperator::ALGetFileName(char* pPath)
{	
	std::string strRet;
	if (pPath == nullptr)
	{
		return strRet;
	}
	strRet = pPath;
	std::size_t found = strRet.find_last_of("/\\");
	strRet = strRet.substr(found + 1);
	return strRet;
}


std::string FileOperator::ALGetProgramDir()
{
	TCHAR exeFullPath[MAX_PATH] = { 0 };
	std::string strPath = "";
	GetModuleFileName(NULL, exeFullPath, MAX_PATH);
	char CharString[MAX_PATH] = { 0 };
	size_t convertedChars = 0;
	wcstombs_s(&convertedChars, CharString, MAX_PATH, exeFullPath, _TRUNCATE);
	strPath = (std::string)CharString;
	int pos = strPath.find_last_of('\\', strPath.length());
	return strPath.substr(0, pos);
}

bool FileOperator::FileExist(std::string strPath)
{
    struct stat buffer;
    return (stat(strPath.c_str(), &buffer) == 0);
}

int FileOperator::GetAllFormatFiles(std::string path, std::vector<std::string>& files, std::string format)
{
    int iFileCnt = 0;
    __int64 hFile = 0;
    struct _finddata_t fileinfo;
    std::string p;

    if (ALIsFile((char*)path.c_str()))
    {
        size_t last_slash_idx = path.rfind('\\');
        if (std::string::npos != last_slash_idx)
        {
            path = path.substr(0, last_slash_idx);
        }
    }

    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
        do {
            if ((fileinfo.attrib & _A_SUBDIR)) {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
                    //string strpathTemp = p.assign(path).append("\\").append(fileinfo.name);
                    //GetAllFormatFiles(strpathTemp, files, format);
                }
            }
            else {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
    return iFileCnt;
}
#pragma once
#include <string>
#include <vector>
class FileOperator
{
public:
	FileOperator();
	~FileOperator();
public:
	bool ALIsFile(char* pPath);
	bool ALDeleteFile(char* pPath);
	std::string ALGetFileName(char* pPath);
    std::string ALGetFileNameWithOutExt(char* pPath);

	bool ALIsDir(char* pPath);
	bool ALDeleteDir(char* pPath);
	std::string ALGetProgramDir();
    bool FileExist(std::string strPath);

    int GetAllFormatFiles(std::string path, std::vector<std::string>& files, std::string format);

};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值