C++ judge file whether exists

复制代码
#include <iostream>

using namespace std;

void fileExist11();
void fileExist12();

int main()
{
    fileExist12();
    return 0;
}

void fileExist12()
{
    string fileName="log.txt";
    FILE *file=fopen(fileName.c_str(),"r");
    if(file!=nullptr)
    {
        cout<<fileName<<" exists!"<<endl;
        fclose(file);
    }
    else
    {
        cout<<fileName<<" not exist!"<<endl;
    }
    cout<<"fileExist12() finished!"<<endl;
}

void fileExist11()
{
    string fileName="log.txt";
    ifstream rFile(fileName.c_str());
    if(rFile.good())
    {
        cout<<fileName<<" existed!"<<endl;
    }
    else
    {
        cout<<fileName<<" not exist!"<<endl;
    }
    cout<<"fileExist11() finished!"<<endl;
}
复制代码
g++ -g -std=c++2a -I. *.cpp -o h1 -lpthread -luuid

.Run compiled result

 

posted @   FredGrit  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2017-02-03 Immediate Window
点击右上角即可分享
微信分享提示