0

_open を数回呼び出す関数があります。

_popen戻る場合、関数が戻る前NULLに呼び出す必要がありますか?_pclose

_pclose電話をかける必要があると思われる場所を 3 つマークしました。

これらの場所のどこに電話する必要があります_pcloseか?

bool theFunction()
{
    FILE* pPipe;
    char buffer[1000];
    if( (pPipe = _popen("dir", "rt")) == NULL )
    {
        //location 1
        _pclose(pPipe);
        return false;
    }

    while(fgets(pipeBuffer, maxBufferSize, pPipe))
    {
        printf(pipeBuffer);
    }

    if( (pPipe = _popen("cls", "rt")) == NULL )
    {
        //location 2
        _pclose(pPipe);
        return false;
    }

    //location 3
    _pclose(pPipe);

    return true;
}
4

2 に答える 2

1

簡単です。パイプを開くことができたが、必要なくなった場合は、パイプを閉じます。そう:

bool theFunction()
{
    FILE* pPipe;
    char buffer[1000];
    if( (pPipe = _popen("dir", "rt")) == NULL )
    {
        return false;
    }

    while(fgets(pipeBuffer, maxBufferSize, pPipe))
    {
        printf(pipeBuffer);
    }

    // The fact that you have to close it here in the middle of nowhere
    // should ring a bell that you need to think about separation of concern 
    _pclose(pPipe);

    if( (pPipe = _popen("cls", "rt")) == NULL )
    {
        return false;
    }

    _pclose(pPipe);
    return true;
}
于 2015-12-03T19:51:46.903 に答える