Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;需要使用.good而不是.eof_C++ - Fatal编程技术网

C++ C++;需要使用.good而不是.eof

C++ C++;需要使用.good而不是.eof,c++,C++,可能重复: 我是一个完全的初学者,我在其中使用了这个函数。eof,但是我被告知这不是一个好主意,因为这在linux中不起作用,我应该使用。很好 那么你能告诉我我会怎么做,我应该如何修改它吗 std::string ex_file_licensing::getUsedLicense(const std::string &FlexLMfileName){ std::ofstream openFile; std::string line; s

可能重复:

我是一个完全的初学者,我在其中使用了这个函数。eof,但是我被告知这不是一个好主意,因为这在linux中不起作用,我应该使用。很好 那么你能告诉我我会怎么做,我应该如何修改它吗

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;
    }
std::string-ex\u-file\u-license::getUsedLicense(const-std::string&FlexLMfileName){
std::ofstreamopenfile;
std::字符串行;
std::string tempString;
std::字符串测试结果;
大小\u t开始位置\u位置;
大小\u t停止\u位置;
std::string retour=“”;
filebuf fb;
fb.open(FlexLMfileName.c_str(),ios::in);
istream toOpen(和fb);
if(toOpen.eof()){
而(!toOpen.eof()){
getline(toOpen,line);
如果(line.find(“Checkout”)!=std::string::npos){
start_pos=line.find(“:”);
tempString=line.substr(开始位置+1);
stop_pos=tempString.find(“/”);
testResult=tempString.substr(开始位置、停止位置);
}
如果(line.find(“FLEXnet”)!=std::string::npos){
start_pos=line.find(“FLEXnet”);
tempString=line.substr(开始位置+1);
stop_pos=tempString.find(“特征”);
testResult=tempString.substr(开始位置、停止位置);
}
不能使用这样的循环:

while (getline(toOpen, line)) {
   ...
}

两者都不使用:流有一个内置的布尔转换操作符,因此您可以简单地用
if(toOpen)
while(toOpen)
测试它,它就可以工作了


顺便说一句,您的
if
条件看起来是错误的,因为它只在文件流达到EOF字符时才执行其中的代码。

我认为
if(toOpen.EOF()){
是一个bug,在任何OSyep上,if条件都是错误的。嗨,如果我把
while(toOpen)
安装
while(!toOpen.EOF())
它会给出相同的结果吗?不完全一样,仅仅因为在您开始消费之前流是正常的,并不意味着它将永远正常。您需要在每次提取后进行测试,这就是为什么@sth建议
而(getline(…)