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; }
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(); }
static void skipCommentsAndReadValue(char *headerLine, std::ifstream& infile){ do { readLine(headerLine, infile); } while( !infile.eof() && !infile.bad() && isComment(headerLine) ); }
/** 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; }
// 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'); } }
void FileReader::closeInputFileStream(const std::string &filePath, std::ifstream &inputFileStream) { inputFileStream.close(); if (inputFileStream.bad()) { MS_throw( FileCloseException, ERR_CLOSE_INPUT_FILE_STREAM_BEGIN + filePath + ERR_CLOSE_INPUT_FILE_STREAM_END ); } }
void FileReader::readFromInputFileStream(const std::string &filePath, std::ifstream &inputFileStream, char *buffer) { inputFileStream.read(buffer, BUFFER_SIZE); if (inputFileStream.bad()) { MS_throw( InvalidInputException, ERR_READ_DATA_FROM_INPUT_FILE_STREAM_BEGIN + filePath + ERR_READ_DATA_FROM_INPUT_FILE_STREAM_END ); } }
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; }
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; } }
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; } }
/* 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*/ }
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; }
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; }
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{ break;//while loop ents } flag = 0; } // while loop ends // convert to lower case? if (bLowerCase){ for (int i=0; i < static_cast<int>(szInputString.length()); i++) szInputString[i] = tolower(szInputString[i]); } break; } } return true; }
// 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]; string p = ddd_line.after(0); if (!p.empty() || c == ':') pattern = p; if (pattern.empty() || (c == ':' && command_no == atoi(pattern.chars())) || (c == '/' && (pattern.empty() || in.contains(pattern)))) { // Report line char buffer[256]; sprintf(buffer, "%4d", command_no); std::ostringstream os; os << buffer << " " << in << "\n"; put(os); if (c == '/' || !pattern.empty()) { // Stop here scanning = false; scan_start = current; command_no_start = command_no - 1; } } } if (!scanning) { last_input = scan_start; initializing = false; // Read command from DDD if (last_output.contains('\n')) { string prompt = last_output.after('\n', -1); if (!prompt.empty() && !prompt.contains('\\', 0)) { if (prompt.contains('(')) prompt = prompt.from('(', -1); last_prompt = prompt; } } if (!last_output.contains(last_prompt, -1)) put(last_prompt); last_output = ""; char *s = fgets(buffer, sizeof buffer, stdin); if (ignore_next_input) { s = fgets(buffer, sizeof buffer, stdin); ignore_next_input = false; } if (s == 0) exit(EXIT_SUCCESS); ddd_line = buffer; if (ddd_line.contains('\n', -1)) ddd_line = ddd_line.before('\n', -1); if (echoing && !ddd_line.empty() && !isalpha(ddd_line[0])) put(ddd_line + "\r\n"); if (ddd_line.contains('q', 0)) exit(EXIT_SUCCESS); if ((ddd_line.contains("list ", 0) || ddd_line.contains("l ", 0)) && (ddd_line.contains(" 1,") || ddd_line.contains(":1,") || ddd_line.contains(" 1-"))) { // Send the log file instead of a source if (echoing) put(ddd_line + "\r\n"); std::ifstream is(logname.chars()); int line = 1; bool at_start_of_line = true; std::ostringstream os; for (;;) { char c; is.get(c); if (is.eof()) break; if (at_start_of_line) { os << line << '\t'; at_start_of_line = false; } os << c; if (c == '\n') { line++; at_start_of_line = true; } } put(string(os)); scanning = false; } } if (!scanning && ddd_line == ".") { std::ostringstream os; os << "Expecting " << command_no << " " << quote(in) << "\n"; put(os); log.seekg(scan_start); command_no = command_no_start; } else if (!scanning && ddd_line == "?") { put(usage); log.seekg(scan_start); command_no = command_no_start; } else if (ddd_line == in || ddd_line == "!" || ddd_line.empty()) { // Okay, got it scanning = false; } else if (!scanning) { // Bad match: try to find this command in the log expecting = in; scanning = true; wrapped = false; } } } if (log.eof() || log.fail()) { if (scanning && wrapped) { // Nothing found. Don't reply. if (echoing && (ddd_line.empty() || isalpha(ddd_line[0]))) put(ddd_line + "\r\n"); scanning = false; log.clear(); log.seekg(scan_start); out = ""; command_no = command_no_start; } else if (initializing) { // No prompt found std::cerr << logname << ": invalid or incomplete log\n"; exit(EXIT_FAILURE); } else { // Try from the start wrapped = true; out_seen = false; log.clear(); log.seekg(0); command_no = 0; } } } }
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 cerr<<"Cannot write to model file\n"; for (UINT ind = 0; ind < totRpNum; ind++) { if (X[ind].F != NULL) delete[] X[ind].F; } delete[] X; delete[] w; delete [] B; if(lineNum != totRpNum) return -1; return 0; }
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; }