C++ 使用try-catch块从文件中读取异常C++;
我想阅读一定长度的行并解析它们。为了解析它们,我使用异常来处理特定的错误输入,但是当我捕捉到错误时,我的缓冲区会被错误输入填满,并且下一个getline命令会在缓冲区中放入乱码,即使文件中仍然有有效输入 这是我的密码: (C++ 使用try-catch块从文件中读取异常C++;,c++,validation,input,try-catch,inputstream,C++,Validation,Input,Try Catch,Inputstream,我想阅读一定长度的行并解析它们。为了解析它们,我使用异常来处理特定的错误输入,但是当我捕捉到错误时,我的缓冲区会被错误输入填满,并且下一个getline命令会在缓冲区中放入乱码,即使文件中仍然有有效输入 这是我的密码: (inStream属于istream类型,因为如果文件无效,我将从cin读取) 你必须提供更多的信息。当在Fedora上使用g++5.1.1进行编译时,下面的代码工作得很好。显然,我必须创建一些缺失的基础设施来编译和运行它,但我不认为我有太多的自由 #include <io
inStream
属于istream
类型,因为如果文件无效,我将从cin
读取)
你必须提供更多的信息。当在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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义最小值(x,y)(x0)
{
字符串较小=line.substr(0,最小值(MAX_line_LENGTH,line.LENGTH());
line=line.substr(min(MAX_line_LENGTH,line.LENGTH());
试一试{
解析(较小、流内、流外);
}
捕获(const DoneError&e){
Outstream这或多或少是我所做的,我读取一行,然后抛出我从解析函数(包含更多局部变量)中生成的异常,然后读取下一个异常。我创建了一个包含两行的文件:bad command(应该失败)
finish(应该通过)
坏命令
是按应该的方式读取的,抛出正确的异常并打印我需要的内容,但当它读取完成时,缓冲区中有)Zu
(胡言乱语因调用而异)-胡言乱语。这是为什么?为什么下一次读取会用胡言乱语填充我的缓冲区,而不是完成?我检查了P.S,并且已经在catch块缓冲区中,缓冲区的内容已更改为“\030”或其他垃圾-它在进入catch块时会更改(我在签名上放了一个断点,它仍然很好,但当我进入块时它改变了)。这可能是带有badbit
标志的东西吗?因为使用clear()
或ignore()
没有多大帮助。顺便问一下,我还需要提供什么信息?我打开一个文件(不重定向)。在原始注释中添加了部分代码。我没有观察到您报告的故障。如果输入文件包含的行数小于MAX_LINE_LENGTH,则显示异常处理按预期工作(在我提供的代码中)。如果输入行长度超过最大行长度,则流将设置其故障位并按预期退出循环。请参阅编辑的代码(如上),我明白了。。因此,如果我希望行
包含与缓冲区
相同的内容(最大行长度
字符,它管理读取)我应该怎么做?如果行的长度是x
乘以MAX\u line\u length
,我需要做什么更改才能在x
迭代中读取它?有一个std::getline()
function,它在istream
s和string
s上运行,用它来代替memberfunction。我知道,但正如我所说的,每次迭代我都需要读取最大行字符数,即使行包含的字符数更多,std::getline()
并没有限制它读取的字符数,它只读取整行。
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;
#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;
}