Linux下,C++判断指定路径下,是否存在wps打开的文件

1、最近要判断某文件夹下,是否存在wps打开的word的文件,基本思路是,利用wps打开word文件时,会在同级目录下生成一个以 .~ 开头的临时文件,word文件关闭后,临时文件自动会被删除。所以只要判断目录下,是否存在以 .~ 开头的文件即可。

2、代码实现方法

#include <QDateTime>
#include <QString>
#include <QVariant>
#include <QtMath>
#include <QCoreApplication>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
using namespace std;

bool FileExisted(string keyword)
{
    // 获取文件路径
    QString Path = QCoreApplication::applicationDirPath();
    int first = Path.lastIndexOf("/");
    Path = Path.left(first);
    QString filepath = Path.append("/data/report/");

    struct dirent* ptr = 0;
    DIR *pDir = opendir(filepath.toStdString().c_str());
    if(!pDir)
       return false;
	// 循环读取文件名
    while((ptr = readdir(pDir)) != 0)
    {
        if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0))
           continue;
        // strstr如果没有找到,则会返回nullptr
        if (strstr(ptr->d_name, keyword.c_str()) != nullptr) {
            return true;
        }
    }
    closedir(pDir);
    return false;
}

3、调用

if (FileExisted(".~")) {
	// 存在wps打开的文件时,处理以下内容
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值