繁体   English   中英

如何将const char *注入FILE类

[英]How to inject a const char* in to the FILE class

如何修改此函数以从字符串而不是文件中读取? glfxin显然是一个FILE对象。 如果我能以某种方式将一个const char *注入到FILE类中,我将被设置。 有任何想法吗?

bool glfxParseEffectFromFile( int effect, const char* file )
{
    bool retVal=true;

    fopen_s(&glfxin, file, "r");
    if(glfxin==NULL) {
        gEffects[effect]->Log()<<"Cannot open file "<<file<<endl;
        gEffects[effect]->Active()=false;
        return false;
    }
    try {
        //glfxdebug=1;
        gEffect=gEffects[effect];

        string fname(file);
        size_t lastSlash=fname.find_last_of('/')+1;
        size_t lastBackSlash=fname.find_last_of('\\')+1;
        lastSlash=max(lastSlash, lastBackSlash);
        gEffect->Dir()=fname.substr(0, lastSlash);

        glfxrestart(glfxin);
        glfxset_lineno(1);
        glfxparse();
    }
    catch(const char* err) {
        gEffect->Log()<<err<<endl;
        gEffect->Active()=false;
        retVal=false;
    }
    catch(const string& err) {
        gEffect->Log()<<err<<endl;
        gEffect->Active()=false;
        retVal=false;
    }
    catch(...) {
        gEffect->Log()<<"Unknown error occurred during parsing of "<<file<<endl;
        gEffect->Active()=false;
        retVal=false;
    }

    fclose(glfxin);
    return retVal;
}

在符合POSIX的系统上,您可以使用fmemopen()函数将字符数组作为FILE *对象打开。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM