Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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++_C - Fatal编程技术网

C++ 处理导致核心转储的文件结束错误

C++ 处理导致核心转储的文件结束错误,c++,c,C++,C,当前的问题是,当文件读取例程到达文件行的末尾时,由于内核转储,它会导致我的程序崩溃。发生这种情况时,正好在文件的末尾 完成生产线结束后,继续该计划的最佳实践方式是什么 这就是我所做的 std::string rd_file(std::string fname,uint16_t lnum) { ifstream PTS_file; std::string line; uint16_t lcnt=0; PTS_file.open(fname); if (!PTS_file.is

当前的问题是,当文件读取例程到达文件行的末尾时,由于内核转储,它会导致我的程序崩溃。发生这种情况时,正好在文件的末尾

完成生产线结束后,继续该计划的最佳实践方式是什么

这就是我所做的

std::string rd_file(std::string fname,uint16_t lnum)
{
  ifstream PTS_file;
  std::string line;
  uint16_t lcnt=0; 
  PTS_file.open(fname);
  if (!PTS_file.is_open())
  {
       cout<<" File "<<fname<<" is not found "<<endl;

  }
  else if (PTS_file.eof())
  {
       cout<< " End of file data..."<<endl;
  }
  else 
  {
    while(!PTS_file.eof() && lcnt <=lnum)
    {
          getline(PTS_file,line);
          lcnt++;
          if ( lcnt == lnum)
          {
             PTS_file.close();
             return line;
          }

     }

   }

 }
std::string rd_文件(std::string fname,uint16_lnum)
{
IFU文件;
std::字符串行;
uint16_t lcnt=0;
PTS_file.open(fname);
如果(!PTS_file.is_open())
{

cout如果您的函数曾经到达函数的末尾,则会出现问题,因为您没有return语句。这本身就是导致未定义行为的原因

稍微更改一下函数,以便在末尾有一个有效的
return
语句

std::string rd_file(std::string fname,uint16_t lnum)
{
   ifstream PTS_file;
   std::string line;
   uint16_t lcnt=0; 
   PTS_file.open(fname);
   if (!PTS_file.is_open())
   {
      cout<<" File "<<fname<<" is not found "<<endl;
   }
   else if (PTS_file.eof())
   {
      cout<< " End of file data..."<<endl;
   }
   else 
   {
      // This block is refactored and simplified.
      while (lcnt < lnum && std::getline(PTS_file,line) )
      {
         lcnt++;
      }
   }

   return line;
}
std::string rd_文件(std::string fname,uint16_lnum)
{
IFU文件;
std::字符串行;
uint16_t lcnt=0;
PTS_file.open(fname);
如果(!PTS_file.is_open())
{

我认为这不需要标记为
c
。还建议使用
while(getline
…而不是
while(eof
,因为后者意味着最后一行可能会被乱码/重复很多次,但lnum应该是执行读取的那一行,这就是为什么它是函数的一个参数。比如,我只想读取文件的第23行。您建议的while循环不具备获取第23行(lnum)的条件@SamGomari,
while
循环的条件确保如果文件中有23行或更多行,则返回文件的第23行。如果少于23行,则返回文件的最后一行。如果这是不可接受的,则需要在末尾添加一个检查,并返回对您的应用有意义的任何内容应用程序。例如,
if(lcnt
将返回一个空字符串。@RSahu实际上我的代码工作正常,碰巧错误是由其他原因造成的。谢谢R Sahu。