加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用C代码检查是否存在txt文件

发布时间:2020-12-16 10:55:20 所属栏目:百科 来源:网络整理
导读:首先,我要确定我的文件夹目录中有文本文件.我使用visual studio,这是我的源代码编译的地方. 下面的代码应该说明它不起作用的原因.在视觉工作室. int main( const int argc,const char **argv ){ char usrMenuOption; const char *cFileName = argv[ 1 ]; che
首先,我要确定我的文件夹目录中有文本文件.我使用visual studio,这是我的源代码编译的地方.

下面的代码应该说明它不起作用的原因.在视觉工作室.

int main( const int argc,const char **argv )
{
    char usrMenuOption;
    const char *cFileName = argv[ 1 ];
    checkName( cFileName );  // supplying the checkName function with contents of argv[1]

    usrMenuOption = getUsrOption();  // calling another function

    fgetc(stdin);
         return 0;
}
ifstream *openInputFile( const char *cFileName )
{
    // this function might be the pronblem.
    ifstream *inFile;
    inFile = new ifstream; 
    inFile->open( cFileName,ios::in );
    return inFile;
}
bool checkName( const char *cFileName )
{
    // it works fine if i use a regular ifstream obj and not the one from the function
    ifstream *inFile;
    inFile = openInputFile( cFileName );
    inFile->open( cFileName,ios::in );

    if ( inFile->good() )
    { 
        return true;
    }
    else
    { 
        cout << '"' << cFileName << '"' << ": File does not exist! " << endl;
        return false;
    }         
}

如果我为ifstream使用非指针对象,它确实有效.
但是我需要使用我所做的功能以这种方式打开我的所有输入文件.
我有点困惑,因为我没有在dev-cpp编译这个问题

解决方法

我总是检查ifs.is_open()ifs是ifstream.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读