0

重複の可能性:
ループ条件内の iostream::eof が間違っていると見なされるのはなぜですか?

私はまったくの初心者で、.eof を使用するこの関数を持っていますが、それは Linux では機能せず、.good を使用する必要があるため、良い考えではないと言われました。そして、それをどのように変更する必要がありますか

std::string ex_file_licensing::getUsedLicense(const std::string &FlexLMfileName){

        std::ofstream openFile;
        std::string line;
        std::string tempString;
        std::string testResult;
        size_t start_pos;
        size_t stop_pos;
        std::string retour ="";
        filebuf fb;
        fb.open (FlexLMfileName.c_str(),ios::in);
        istream toOpen(&fb);


        if(toOpen.eof()){
            while(!toOpen.eof()){
                getline(toOpen,line);
                if(line.find("Checkout") != std::string::npos ){   
                    start_pos = line.find(":"); 
                    tempString = line.substr(start_pos+1);
                    stop_pos = tempString.find("/");
                    testResult = tempString.substr(start_pos, stop_pos);
                }
                else if (line.find("FLEXnet") != std::string::npos ){   
                    start_pos = line.find("FLEXnet"); 
                    tempString = line.substr(start_pos+1); 
                    stop_pos = tempString.find("Feature");
                    testResult = tempString.substr(start_pos, stop_pos);
                }
                cout << testResult << endl;
                retour.append(testResult);
            }

            fb.close();
        }

        return retour;
    }
4

2 に答える 2

1

次のようなループを使用します。

while (getline(toOpen, line)) {
   ...
}
于 2012-06-06T09:44:15.153 に答える
0

if (toOpen)どちらも使用しない: ストリームにはブール値への変換演算子が組み込まれているため、 orwhile(toOpen)を使用して簡単にテストでき、動作します .

ちなみに、ifファイルストリームが EOF 文字に達した場合にのみコードを実行するため、条件は間違っているように見えます。

于 2012-06-06T09:45:00.250 に答える