Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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++ GETLIN失败,以“成功”:_C++_Getline - Fatal编程技术网

C++ GETLIN失败,以“成功”:

C++ GETLIN失败,以“成功”:,c++,getline,C++,Getline,我在java中用C++返回了C++,这已经让我保持了两个星期。我已经看到了许多GETLIN问题的帖子,这些都让我添加了许多错误,试图找出错误所在。我已经发现了坏的、失败的和EOF位,但仍然有相同的问题。GETLIN不填充字符串或进入while。环 我不知道这是否重要,但我正在用netbeans在Win7 PC上编写代码,并在运行raspbian Debian的rasberry pi上编译/运行 我所要做的就是使用getline!读取一个简单的文本文件!应该是基本的“使用双关语:- 我已尝试使用U

我在java中用C++返回了C++,这已经让我保持了两个星期。我已经看到了许多GETLIN问题的帖子,这些都让我添加了许多错误,试图找出错误所在。我已经发现了坏的、失败的和EOF位,但仍然有相同的问题。GETLIN不填充字符串或进入while。环 我不知道这是否重要,但我正在用netbeans在Win7 PC上编写代码,并在运行raspbian Debian的rasberry pi上编译/运行

我所要做的就是使用getline!读取一个简单的文本文件!应该是基本的“使用双关语:- 我已尝试使用Unix和ASCII行结尾0x0A和0x0D0A创建文本文件-没有任何区别。我使用创建文本文件的相同用户ID运行该程序,因此没有权限问题

Getline只设置失败位…不是eof,而是perror报告成功,我在工作中没有得到任何数据

下面是导致错误的大多数类-带有许多调试注释。该文件只是一个fstream的cos,在未来的版本中,我可能希望打开它以进行输出。但是,正如您从输出中看到的,在这里,我肯定是使用ios::in打开文件。问题发生在getVal中从未输入的getline循环ue成员函数。 我没有包括构造函数,但它只将一些标志设置为false。调用程序MJAppl只调用MJConfigFile::getValue log_dir,false

string MJConfigFile::getValue ( string in_key, bool in_restart ) {
    bool   found   = false ;
    string work    , ret   ;
    int    key_len = in_key.length() , loop_count = 0 ;
    //char   workbuf [ 200 ] ;

    cout << "MJConfigFile::getValue entry for key " << in_key << " \n" ;
    cout << "File name is " << getFName () << "\n" ;
    if ( in_restart || !openForInput ) {
        theFile . close () ;
        open ( false ) ;        
    }
    if ( theFile . is_open ()) {
        cout << "File " << fullFileName << " is open." << "\n";
    } else {
        perror ( "Error while opening file " ) ;
    }
    while ( getline ( theFile, work )) {
        if (loop_count++ > 5 ) return "Stopped \n" ;
        cout << "Line '" << work << "'\n";
        if ( in_key.compare ( work ) == 0 ) {
            ret = work.substr ( key_len + 1 ) ;
            break ;
        }
    }
    perror ( "Error reading file. " ) ;
    cout << "Line after loop '" << work << "'\n";
    if ( theFile.bad() | theFile.fail() | theFile.eof()) {
        if ( theFile . bad () ) cout << "Bad  file." << endl ;
        if ( theFile . fail() ) cout << "Fail file." << endl ;
        if ( theFile . eof () ) cout << "eof  file." << endl ;

    }
    return ret ;
}

void MJConfigFile::open () {
    this -> open ( false ) ;
}
void MJConfigFile::open ( bool in_for_output ) {    

    if ( in_for_output ) {
        theFile . open ( fullFileName.c_str (), ios::out ) ;
        if ( theFile . is_open ()) {
            openForOutput = true ;
            cout << "File is open for output. \n" ;
        }
    } else {
        theFile . open ( fullFileName.c_str (), ios::in ) ;
        if ( theFile . is_open ()) { 
            openForInput  = true ;
            cout << "File is open for input. \n" ;
        }
    }    
    if ( openForInput || openForOutput ) {
        exists   = true ;
    }
}
下面是一个示例输入文件:

    Aaaaaa
    Bbbbbb
    Cccccc

我已经准备好放弃并返回java,因此我非常感谢您的帮助。

为了防止任何人再次遇到这种特殊的失败/成功条件,问题是我在同一个fstream上调用了open两次。open似乎成功了,但getline不会运行,因为流的失败位已设置。不是吗hanks Igor。

你的问题不能满足A的要求。你所看到的帮助是创建一个要包含在你的问题中的指令。看起来过于复杂,与语言风格相反。你是否先找一个例子来熟悉自己在C++中如何完成?可能的副本没有理由相信fstream上的操作会将errno设置为错误代码。另外,请提供一个示例,在调用getline之前是否检查了失败位?
    Aaaaaa
    Bbbbbb
    Cccccc