本文整理汇总了C++中ifstream::exceptions方法的典型用法代码示例。如果您正苦于以下问题:C++ ifstream::exceptions方法的具体用法?C++ ifstream::exceptions怎么用?C++ ifstream::exceptions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ifstream
的用法示例。
在下文中一共展示了ifstream::exceptions方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CheckAndOpenFileForReading
void CRunner::CheckAndOpenFileForReading(ifstream & file, const string& fileName)
{
file.open(fileName);
file.exceptions(ifstream::badbit);
if (!file.is_open())
{
throw ifstream::failure(MESSAGE_FAILED_OPEN + fileName + MESSAGE_FAILED_OPEN_FOR_READING);
}
}
示例2: readCSVfile
int CSVop::readCSVfile(ifstream &file)
{
int err;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
if(!file.is_open()) return -1;
err = tab.loadTable(file, CSVparams.delim, CSVparams.ignorebefore, CSVparams.ignoreafter);
if(0!=err) return err;
tab.rmquotmarks();
return 0;
}
示例3: readSCHfile
int KiCadSCH::readSCHfile(ifstream &file)
{
int err;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
if(!file.is_open()) return -1;
err = tab.loadTable(file, " ");
if(0!=err) return err;
tab.rmquotmarks();
return 0;
}
示例4: evaluateKeyWord
void evaluateKeyWord(ifstream &infile ///< stream with parameter file
, string keyword, ///< keyword
HostParameters &hostParamters, Parameters &khParamters) {
infile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
if (keyword == "USEOPENCL") {
infile >> hostParamters.useOPENCL;
} else if (keyword== "CLTARGET") {
infile >> hostParamters.targetPlatform;
infile >> hostParamters.targetDevice;
} else if (keyword == "PNUMBER") {
示例5: readWorldFile
/* Tries to read a world file from the specified stream. On success, returns
* true and updates the input parameters to mark the type of the world and
* the world contents. On failure, returns false, but may still modify the
* input parameters.
*/
static bool readWorldFile(ifstream& input, Grid<double>& world,
WorldType& worldType) try {
/* Enable exceptions on the stream so that we can handle errors using try-
* catch rather than continuously testing everything.
*/
input.exceptions(ios::failbit | ios::badbit);
/* The file line of the file identifies the type, which should be either
* "terrain" or "maze."
*/
string type;
input >> type;
if (type == "terrain") {
worldType = TERRAIN_WORLD;
} else if (type == "maze") {
worldType = MAZE_WORLD;
} else return false;
/* Read the size of the world. */
int numRows, numCols;
input >> numRows >> numCols;
if (numRows <= 0 || numCols <= 0 ||
numRows >= kMaxRows || numRows >= kMaxCols) {
return false;
}
world.resize(numRows, numCols);
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
double value;
input >> value;
/* Validate the input based on the type of world. */
if (worldType == MAZE_WORLD) {
if (value != kMazeWall && value != kMazeFloor) {
return false;
}
} else /* worldType == TERRAIN_WORLD */ {
if (value < 0.0 || value > 1.0) {
return false;
}
}
world[row][col] = value;
}
}
return true;
} catch (...) {
/* Something went wrong, so report an error. */
return false;
}
示例6: openInputFile
void openInputFile(const string fileName, ifstream &handle){
handle.exceptions(ifstream::failbit | ifstream::badbit);
try{
handle.open(fileName);
}
catch (const ifstream::failure &e){
cerr << e.what() << endl;
cerr << "File " << fileName << " cannot be opened!" << endl;
terminate();
}
}
示例7: readWorldFile
/*
* Tries to read a world file from the specified stream. On success, returns
* true and updates the input parameters to mark the type of the world and
* the world contents. On failure, returns false, but may still modify the
* input parameters.
*/
static bool readWorldFile(ifstream& input, Grid<double>& world, WorldType& worldType) {
try {
// Enable exceptions on the stream so that we can handle errors using try-
// catch rather than continuously testing everything.
input.exceptions(ios::failbit | ios::badbit);
// The file line of the file identifies the type, which should be either
// "terrain" or "maze."
string type;
input >> type;
if (type == "terrain") {
worldType = TERRAIN_WORLD;
} else if (type == "maze") {
worldType = MAZE_WORLD;
} else {
cerr << "world file does not contain type (terrain/maze) as first line." << endl;
return false;
}
// Read the size of the world.
int numRows, numCols;
input >> numRows >> numCols;
if (numRows <= 0 || numCols <= 0 ||
numRows >= kMaxRows || numRows >= kMaxCols) {
cerr << "world file contains invalid number of rows/cols: "
<< numRows << "," << numCols << endl;
return false;
}
world.resize(numRows, numCols);
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
double value;
input >> value;
if (input.fail()) {
cerr << "Illegal input file format; row #" << (row+1)
<< "does not contain " << numCols << " valid numbers" << endl;
return false;
}
// Validate the input based on the type of world.
if (worldType == MAZE_WORLD) {
if (value != kMazeWall && value != kMazeFloor) {
cerr << "world file contains invalid square value of " << value
<< ", must be " << kMazeFloor << " or " << kMazeWall << endl;
return false;
}
} else { // worldType == TERRAIN_WORLD
if (value < 0.0 || value > 1.0) {
cerr << "world file contains invalid terrain value of " << value
<< ", must be 0.0 - 1.0" << endl;
return false;
}
}
world[row][col] = value;
}
}
return true;
} catch (...) {
// Something went wrong, so report an error.
cerr << "exception thrown while reading world file" << endl;
return false;
}
}