使用try-catch块从文件中读取行以获取异常C ++

用户名

我想读取一定长度的行并进行解析。为了解析它们,我使用异常来处理特定的错误输入,但是每当我遇到一个错误时,我的缓冲区就会被错误的输入填充,即使文件中仍然有有效的输入,下一个getline命令也会在其中输入乱码。

这是我的代码:

inStream是类型的,istream因为如果文件无效,我将从中读取cin

char buffer[MAX_LINE_LENGTH];
string line;
while (inStream.getline(buffer,MAX_LINE_LENGTH)) {
    line=string(buffer);

    try {
        parse(line, inStream, outStream);
    }

    catch(const DoneError& e){
        outStream<<e.what();
        return 0;
    }

    catch(const MyException& e) {
        outStream<<e.what();
    }

    //invalid_argument or out_of_range at stod & stoi
    catch (const logic_error& e)    {
        outStream<<BadParameterExeption().what();
    }

    catch(const exception& e){
        outStream<<e.what();
    }
}

在我第一次捕获来自parse(已经在catch块中)的异常时,buffer它填充了垃圾而不是原始内容,循环的下一次迭代也填充buffer了垃圾而不是输入。我尝试使用clear(),并ignore(MAX_LINE_LENGTH, '\n')在catch块之后,但它并没有帮助。我能做些什么?我最多要读取MAX_LINE_LENGTH个字符,以便在一行中获得的字符数最多时,可以在下一次迭代中读取它。

我从文件中读取(不重定向),方法如下:

ifstream  inpFile;
ofstream outFile;

/* I receive the files' names via argv, some are input some are output
 * (order is fixed but not the number)
 * /.prog outpu1 input1 output2 input2 (and so on)
 * degenerated example on how It works:
 *    for(int i=argc-1; i>0; i-=2){
 *       inpFile.open(argv[i]);
 *       if(inpFile.is_open()) return true;
 *    }
 */
if (!initFiles (argc, argv, inpFile, outFile))
    return 1;

istream &inStream = (inpFile.is_open()) ? inpFile : cin;
ostream &outStream = (outFile.is_open()) ? outFile : cout;
辛克

您必须提供更多信息。当在Fedora上使用g ++ 5.1.1进行编译时,以下代码工作得很好。显然,我必须创建一些缺少的基础结构才能进行编译和运行,但是我认为我没有太多的自由。

#include <iostream>
#include <fstream>
#include <exception>
#include <stdexcept>
#include <string>

using namespace std;

#define min(x, y) (x < y) ? x : y

const int MAX_LINE_LENGTH = 80;

struct DoneError : public exception
{
   const char *what() const throw()
   {
      return "Done Exception";
   }
};

struct MyException : public exception
{
   const char *what() const throw()
   {
      return "MyException";
   }
};

struct BadParameterExeption : public exception
{
   const char *what() const throw()
   {
      return "Bad Parameter";
   }
};

void parse(string l, istream &is, ostream &os)
{
   if (l.find("MyException") != string::npos)
      throw MyException();

   if (l.find("Done") != string::npos)
      throw DoneError();

   if (l.find("logic") != string::npos)
      throw logic_error("You made a logic error");

   if (l.find("BadParameter") != string::npos)
      throw BadParameterExeption();
}


int main(int argc, char **argv)
{
   ifstream inpFile(argv[1]);
   ofstream outFile(argv[2]);

   istream &inStream = (inpFile.is_open()) ? inpFile : cin;
   ostream &outStream = (outFile.is_open()) ? outFile : cout;

   char buffer[MAX_LINE_LENGTH];
   string line;
   try {
      while (getline(inStream, line, '\n'))
      {
          while (line.length() > 0)
          {
             string smaller = line.substr(0, min(MAX_LINE_LENGTH, line.length()));
             line = line.substr(min(MAX_LINE_LENGTH, line.length()));
             try {
                 parse(smaller, inStream, outStream);
             }

          catch(const DoneError& e){
              outStream<<e.what()<<endl;;
              return 0;
          }

          catch(const MyException& e) {
              outStream<<e.what()<<endl;;
          }

    //invalid_argument or out_of_range at stod & stoi
          catch (const logic_error& e)    
          {
              outStream<<BadParameterExeption().what()<<endl;;
          }

          catch(const exception& e){
             outStream<<e.what()<<endl;;
          }
         }
      }
   }
   return 0;
}

由于您并没有真正解释您最终想要做什么,因此您可能必须在处理输入内容方面进行重构。istream.getline()处理原始字符,并且可以“很杂乱”

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章