本文整理汇总了C++中std::ifstream::bad方法的典型用法代码示例。如果您正苦于以下问题:C++ ifstream::bad方法的具体用法?C++ ifstream::bad怎么用?C++ ifstream::bad使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::ifstream
的用法示例。
在下文中一共展示了ifstream::bad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: read
virtual bool read(std::ifstream& in, int n, bool binary)
{
resize(n);
if (binary)
{
in.read((char*)data, n * sizeof(T));
if (in.eof() || in.bad())
{
resize(0);
return false;
}
}
else
{
int i = 0;
std::string line;
while(i < dataSize && !in.eof() && !in.bad())
{
std::getline(in, line);
std::istringstream ln(line);
while (i < n && ln >> data[i])
++i;
}
if (i < n)
{
resize(0);
return false;
}
}
return true;
}
示例2: read
bool File::read(std::ifstream& stream)
{
CwdGuard cg(physical_dir_);
stream.open(ACE_TEXT_ALWAYS_CHAR(physical_file_.c_str()),
ios::binary | ios::in);
return !stream.bad() && !stream.fail();
}
示例3: skipCommentsAndReadValue
static
void skipCommentsAndReadValue(char *headerLine, std::ifstream& infile){
do {
readLine(headerLine, infile);
} while( !infile.eof() && !infile.bad() &&
isComment(headerLine) );
}
示例4: LoadAndValidateHeaders
/**
Loads and checks both bitmap file and info headers.
@param[in] FileStream The input file stream.
@param[out] FileHeader The bitmap file header read from the file.
@param[out] InfoHeader The bitmap info header read from the file.
@return One of #BitmapLoadStatus codes.
*/
static BitmapLoadStatus LoadAndValidateHeaders(std::ifstream& FileStream, BitmapFileHeader* FileHeader,
BitmapInfoHeader* InfoHeader)
{
FileStream.read(reinterpret_cast<char*>(FileHeader), sizeof(*FileHeader));
if (FileStream.bad()) {
return INVALID_FILE_HEADER;
}
if (!ValidateFileHeader(*FileHeader)) {
return INVALID_FILE_HEADER;
}
FileStream.read(reinterpret_cast<char*>(InfoHeader), sizeof(*InfoHeader));
if (FileStream.bad() || !ValidateInfoHeader(*InfoHeader)) {
return INVALID_INFO_HEADER;
}
return LOAD_SUCCESS;
}
示例5: readLine
// read a line in headerLine and discards the any remaining characters
static
void readLine(char *headerLine, std::ifstream& infile){
infile.getline( headerLine, kMaxNameLength );
// still some characters left on the current line ?
if ( !infile.eof() && !infile.bad() && infile.rdstate() & std::ios::failbit){
// clear failbit
infile.clear(infile.rdstate() & ~std::ios::failbit);
// discard characters.
char c;
while( infile.get(c) && c != '\n');
}
}
示例6: throw
template<class T> void exec_input(T ®){
static bool read_eof = false;
fin >> reg;
if( fin.bad() ) {
throw (std::string)"fatal Error:データ読み込みエラー";
}
if( fin.eof() ){
if(read_eof)throw (std::string)"ファイルサイズを超えて読もうとしています";
else {
std::cerr << "ファイルの内容を全て読みました" << std::endl;
read_eof = true;
}
}
return;
}
示例7: read
bool compactIntArray::read(std::ifstream& fin)
{
fin.read((char*)&size, sizeof(size));
fin.read((char*)&wordsize, sizeof(wordsize));
fin.read((char*)&innersize, sizeof(innersize));
this->array = new ulong[this->innersize];
fin.read((char*)array, sizeof(*array) * innersize);
if(fin.bad() || fin.fail())
{
return false;
}
else
{
return true;
}
}
示例8: print_lines
static void print_lines(std::ifstream& ifs, std::set<unsigned>& lines)
{
char buf[1024];
unsigned cur_line = 1;
while (!ifs.eof()) {
ifs.getline(buf, sizeof buf);
if (lines.count(cur_line) > 0) {
std::cout << cur_line << ": ";
std::cout << buf << "\n";
}
if (ifs.bad()) {
errs() << "An error occured\n";
break;
}
++cur_line;
}
}
示例9: readInputFile
/* Read input file and build a list of key,value pair */
static
void readInputFile(std::ifstream& infile, tList< tKeyPair > &KeyWordList){
char headerLine[kMaxNameLength];
enum{ KEY, VALUE} state = KEY;
tKeyPair aPair;
for(;;){
skipCommentsAndReadValue(headerLine, infile);
if (infile.eof() || infile.bad())
break;
if (isComment(headerLine))
goto fail;
stripTrailingCR(headerLine);
stripTrailingBlanks(headerLine);
// if a blank line is reached, we stop.
if (headerLine[0] == '\0')
break;
switch (state){
case KEY:
stripKey(headerLine);
aPair.setKey(headerLine);
state = VALUE;
break;
case VALUE:
aPair.setValue(headerLine);
assert(aPair.key() != NULL);
KeyWordList.insertAtBack(aPair);
aPair.clear();
state = KEY;
break;
}
}
return;
fail:
std::cerr
<< "I expected to read a parameter or a value"
"', but reached EOF first" << std::endl;
ReportFatalError( "Error in input file" );
/*NOTREACHED*/
}
示例10: loadbedGraph
std::vector<bedScores> loadbedGraph(std::ifstream& inputStream)
{
// ifstream inputStream;
// inputStream.open(bedpath.c_str());
if (inputStream.bad())
{
cerr << "WTF??. Error loading in loadbedGraph()";
abort();
}
string lineString,header;
stringstream Infostream;
std::getline(inputStream, lineString);
Infostream.str(lineString);
Infostream >>header;
if (header.find("type=bedGraph")==string::npos)
{
cerr << "Error, this is not a valid bedgraph file"<<endl;
abort();
}
bedScores tempscore;
vector<bedScores> returnVector;
while(inputStream.eof()!=true)
{
getline(inputStream, lineString);
//Reset error flags our nothing work.
Infostream.clear();
Infostream.str(lineString);
Infostream >>tempscore.chr;
Infostream >>tempscore.position;
Infostream >>tempscore.end;
Infostream >>tempscore.score;
returnVector.push_back(tempscore);
}
return returnVector;
}
示例11: parse
int ini_file::parse(std::ifstream& f) {
if (!f.is_open() || f.bad())
return 0;
int num = 0;
while (!f.eof()) {
string line;
getline(f, line);
// remove linefeeds
string::const_reverse_iterator it = line.rbegin();
int newlen = line.length();
while (it != line.rend()) {
if ((*it == '\r') || (*it == '\n'))
newlen--;
else
break;
it++;
}
line.resize(newlen);
num += process_line(line);
}
return num;
}
示例12: ReadNextLine
bool CParser::ReadNextLine (std::ifstream& FileInput, int& nLineNum,
std::string& szInputString, const int MAXCHARS,
const std::string& szComment, bool bLowerCase)
// ---------------------------------------------------------------------------
// Function: reads the next line skipping over the comment lines
// and converts all alphabets to lower case if requested
// Input: file istream, line #, string to hold the input line,
// max. # of characters expected in each input line,
// comment character(s) at the beginning of a comment line,
// lowercase conversion option
// Output: updated values of line # and the string
// return value is true if successful
// false if an error state is encountered
// Restriction: Cannot read a line over 256 characters
// ---------------------------------------------------------------------------
{
int flag = 0;
int flag1 =0;
bool bWhSpc = false;
int tokenfound = 1;
const int MAXCH = 1000;
char szInp[MAXCH];
char szTemp [MAXCH];
std::vector<std::string> tokens;
// enough capacity to read and store?
if (MAXCHARS > MAXCH)
return false;
// comment character(s)
int nCLen = static_cast<int>(szComment.length());
// read the line (skip over comment lines)
for(;;)
{
++nLineNum;
FileInput.getline (szInp, MAXCHARS);
// // end-of-file?
// if (FileInput.eof())
// return false;
if (FileInput.fail())
FileInput.clear (FileInput.rdstate() & ~std::ios::failbit);
// unrecoverable error?
if (FileInput.bad())
return false;
// successful read
szInputString = szInp;
GetTokens(szInputString, " ", tokens);
bWhSpc = EatWhiteSpace(szInputString);
if ((szInputString.substr(0,nCLen) != szComment)&& (bWhSpc ==false)){
szInputString = szInp;
GetTokens(szInputString, " ", tokens);
for(int i=0; i< abs(tokens.size()); i++){
std::string temptoken = tokens[i];
if (temptoken == "&")
flag1 = 1;
}
//Filter the comment tokens
// FilterComment(szInputString, szComment);
//if "&" is found continue to read the next line
std::string szTempString = szInputString;
// check if line is continued &
while(flag1 ==1 && tokenfound == 1){
GetTokens(szTempString, " ", tokens);
for(int i=1; i<=abs(tokens.size()); i++){
std::string temptoken = tokens[i-1];
if (temptoken == "&"){
tokenfound = 1;
flag = 1;
}
else{
if(flag==1)
flag = 1;//do nothing token already found
else
tokenfound = 0;
}
}
if(tokenfound ==1){
++nLineNum;
RemoveToken(szInputString);
//- getting more tokens and add to the existing
FileInput.getline (szTemp, MAXCHARS);
// end-of-file?
if (FileInput.eof())
return false;
if (FileInput.fail())
FileInput.clear (FileInput.rdstate() & ~std::ios::failbit);
// unrecoverable error?
if (FileInput.bad())
return false;
// successful read
szTempString = szTemp;
FilterComment(szTempString, szComment);
szInputString+=" ";
szInputString+=szTemp;
}
else{
//.........这里部分代码省略.........
示例13: logplayer
// Simulate a debugger via the DDD log LOGNAME. If a command matches
// a DDD command in LOGNAME, issue the appropriate answer.
void logplayer(const string& logname)
{
// All this is really ugly. Works well as a hack for debugging DDD,
// but not really worth anything else.
static std::ifstream log(logname.chars());
if (log.bad())
{
(void) fopen(logname.chars(), "r");
perror(logname.chars());
exit(EXIT_FAILURE);
}
put("[Playing " + quote(logname) + ". Use `?' for help]\n");
static string out;
static string ddd_line;
static string last_prompt;
static bool initializing = true;
static bool scanning = false;
static bool out_seen = false;
static bool wrapped = false;
static bool echoing = false;
static STREAMPOS scan_start, last_input;
static string expecting;
static int command_no = 0;
static int command_no_start = 0;
static bool ignore_next_input = false;
signal(SIGINT, (SignalProc)intr);
if (setjmp(main_loop_env) != 0)
{
put("Quit\n");
scanning = false;
wrapped = false;
log.clear();
log.seekg(last_input);
command_no = command_no_start;
out = "";
ddd_line = "";
ignore_next_input = true;
}
for (;;)
{
STREAMPOS current = log.tellg();
if (!scanning)
{
scan_start = current;
command_no_start = command_no;
}
// Read line from log
char buffer[65536];
log.getline(buffer, sizeof buffer);
if (!log.fail())
{
string log_line(buffer);
if (out_seen && log_line.contains(" ", 0))
{
// Continuation line
out += unquote(log_line.from('"'));
}
else if (!out.empty())
{
// Send out accumulated output
if (!scanning)
{
if (out.contains(ddd_line, 0))
echoing = true;
put(out);
}
out = "";
}
if (log_line.contains("<- ", 0))
{
assert(out.empty());
// Output line
out = unquote(log_line.from('"'));
out_seen = true;
}
if (out_seen && log_line.contains("-> ", 0))
{
// Handle input
string in = unquote(log_line.from('"'));
if (in.contains('\n', -1))
in = in.before('\n', -1);
command_no++;
if (ddd_line.contains('/', 0) || ddd_line.contains(':', 0))
{
static string pattern;
char c = ddd_line[0];
//.........这里部分代码省略.........
示例14: getAESVMsol
int getAESVMsol(std::ifstream & inpRp, std::ofstream & outMdl,
double C, UINT totRpNum) {
if (inpRp.bad())
return -1;
dataVect_T * X = new dataVect_T[totRpNum];
double *B = new double[totRpNum];
UINT maxF = 0;
UINT lineNum = 0;
while (1) {
string line;
if (getline(inpRp, line)) {
char * lineC = new char[line.length() + 1];
strcpy(lineC, line.c_str());
char * labelC = strtok(lineC, " \t\n");
if (labelC == NULL) {
cout << "Error in input file read\n";
return -1;
}
char *endPtr, *idx, *val;
char label = (char) strtol(labelC, &endPtr, 10);
queue<feat_T> tempQ;
UINT numFeats = 0, fNum = 0;
double fVal = 0;
if (endPtr == labelC || *endPtr != '\0') {
cout << "Error in input file read\n";
return -1;
}
val = strtok(NULL, " \t");
fVal = strtod(val, &endPtr);
if (endPtr == val || (*endPtr != '\0' && !isspace(*endPtr))) {
cout << "Error in input file read\n";
return -1;
}
B[lineNum] = fVal;
while (1) {
idx = strtok(NULL, ":");
val = strtok(NULL, " \t");
if (val == NULL)
break;
fNum = (UINT) strtoll(idx, &endPtr, 10);
if (endPtr == idx || *endPtr != '\0') {
cout << "Error in input file read\n";
return -1;
}
fVal = strtod(val, &endPtr);
if (endPtr == val || (*endPtr != '\0' && !isspace(*endPtr))) {
cout << "Error in input file read\n";
return -1;
}
feat_T tempF;
tempF.fNum = fNum;
tempF.fVal = fVal;
tempQ.push(tempF);
numFeats++;
maxF = max(maxF, fNum);
}
X[lineNum].numFeats = numFeats;
X[lineNum].label = label;
if (numFeats > 0) {
X[lineNum].F = new feat_T[numFeats];
for (UINT ind = 0; ind < numFeats; ind++) {
X[lineNum].F[ind] = tempQ.front();
tempQ.pop();
}
queue<feat_T> empty;
swap(tempQ, empty);
} else
X[lineNum].F = NULL;
delete[] lineC;
} else {
break;
}
lineNum++;
}
maxF++;
double *w = new double[maxF];
if(lineNum == totRpNum) {
// solve AESVM
svmSolverRp(X, w, B, C, totRpNum, maxF);
}
else
cerr<<"Could not read representative set file"<<endl;
// write AESVM model to file
if(outMdl.is_open() && outMdl.good()) {
outMdl<< "solver_type L2R_L1LOSS_SVC_DUAL\n"
<< "nr_class 2\nlabel 1 -1\nnr_feature "<<maxF<<"\n"
<< "bias -1\nw"<<endl;
for(UINT fI = 1; fI < maxF; fI++)
outMdl<< w[fI]<<endl;
}
else
//.........这里部分代码省略.........
示例15: WAssert
bool WLMatLib::MATReader::readHeader( FileInfo_t* const infoIn, std::ifstream& ifs )
{
if( infoIn == NULL )
{
wlog::error( LIBNAME ) << "FileInfo_t is null!";
ifs.seekg( 0, ifs.beg );
return false;
}
infoIn->fileSize = 0;
infoIn->isMatFile = false;
if( !ifs || ifs.bad() )
{
wlog::error( LIBNAME ) << "Problem with input stream!";
ifs.seekg( 0, ifs.beg );
return false;
}
// Check minimum file size
ifs.seekg( 0, ifs.end );
const ifstream::pos_type file_size = ifs.tellg();
infoIn->fileSize = file_size;
wlog::debug( LIBNAME ) << "File size: " << infoIn->fileSize;
if( file_size < 127 )
{
wlog::error( LIBNAME ) << "File size is to small for a MAT file!";
ifs.seekg( 0, ifs.beg );
return false;
}
ifs.seekg( 0, ifs.beg );
// Read description text
char description[117];
ifs.read( description, 116 );
description[116] = '\0';
infoIn->description.assign( description );
wlog::debug( LIBNAME ) << description;
// Read version
ifs.seekg( 8, ifstream::cur );
char version[2] = { 0 };
ifs.read( version, 2 );
if( version[0] != 0x00 || version[1] != 0x01 )
{
wlog::error( LIBNAME ) << "Wrong version!";
ifs.seekg( 0, ifs.beg );
return false;
}
infoIn->isMatFile = true;
// Read endian indicator
char endian[2] = { 0 };
ifs.read( endian, 2 );
if( endian[0] == 'I' && endian[1] == 'M' )
{
infoIn->isLittleEndian = true;
}
else
if( endian[0] == 'M' && endian[1] == 'I' )
{
infoIn->isLittleEndian = false;
WAssert( true, "Big endian is not yet supported!" );
}
else
{
wlog::error( LIBNAME ) << "Unknown endian indicator!";
return false;
}
ifs.seekg( 128 );
return true;
}