// getFILE is a wrapper for getting files bool getFILE(std::fstream &fp, const char* fname, const char* mode) { int writeFile = 0; if (strcmp(mode, "out") == 0) { writeFile = 1; if(writeFile && fexists(fname)) { fprintf(stderr,"File already exists: %s\n",fname); return false; } fp.open(fname, std::ios::out); } else if (strcmp(mode, "app") == 0) fp.open(fname, std::ios::app); else if (strcmp(mode, "in") == 0) fp.open(fname, std::ios::in); if( !fp ) { fprintf(stderr,"Error opening FILE handle for file: %s\n",fname); fp.close(); return false; } return true; }
void createAndOpen(std::fstream& file, std::string name){ if (!exists(name.c_str())){ file.open(name.c_str(), std::fstream::out | std::fstream::binary); file.close(); } file.open(name.c_str(), std::fstream::in | std::fstream::out | std::fstream::binary); }
void SetUp() { graph_file.open(GRAPH_FILENAME, std::fstream::in); ASSERT_FALSE(graph_file.fail()) << "unable to open " << GRAPH_FILENAME; solved_file.open(SOLUTION_FILENAME, std::fstream::in); ASSERT_FALSE(solved_file.fail()) << "unable to open " << SOLUTION_FILENAME; original = fs::DIMACSOriginalImporter<fs::FlowNetwork>(graph_file).read(); ASSERT_NE(original, nullptr) << "DIMACS parse error"; augmented = new fs::FlowNetwork(*original); fs::DIMACSFlowImporter<fs::FlowNetwork>(solved_file, *augmented).read(); }
void createWalletFile(std::fstream& walletFile, const std::string& filename) { walletFile.open(filename.c_str(), std::fstream::in | std::fstream::out | std::fstream::binary); if (walletFile) { walletFile.close(); throw std::runtime_error("Wallet file already exists"); } walletFile.open(filename.c_str(), std::fstream::out); walletFile.close(); walletFile.open(filename.c_str(), std::fstream::in | std::fstream::out | std::fstream::binary); }
void Utils::OpenFStream(const std::string& outputFileName, std::fstream &file, std::ios_base::openmode mode, FCM::PIFCMCallback pCallback) { #ifdef _WINDOWS FCM::StringRep16 pFilePath = Utils::ToString16(outputFileName, pCallback); file.open(pFilePath,mode); FCM::AutoPtr<FCM::IFCMCalloc> pCalloc = Utils::GetCallocService(pCallback); ASSERT(pCalloc.m_Ptr != NULL); pCalloc->Free(pFilePath); #else file.open(outputFileName.c_str(),mode); #endif }
bool StandardFileProvider::open( std::string name, Mode mode ) { ios::openmode om = ios::binary; switch( mode ) { case MODE_UNDEFINED: case MODE_READ: default: om |= ios::in; _seekg = true; _seekp = false; break; case MODE_MODIFY: om |= ios::in | ios::out; _seekg = true; _seekp = true; break; case MODE_CREATE: om |= ios::in | ios::out | ios::trunc; _seekg = true; _seekp = true; break; } _fstream.open( name.c_str(), om ); _name = name; return _fstream.fail(); }
bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode) { std::ios_base::openmode fmode = std::fstream::binary; if (mode == File::Write) { fmode |= (std::fstream::out | std::fstream::trunc); createCache(SNAPPY_CHUNK_SIZE); } else if (mode == File::Read) { fmode |= std::fstream::in; } m_stream.open(filename.c_str(), fmode); //read in the initial buffer if we're reading if (m_stream.is_open() && mode == File::Read) { m_stream.seekg(0, std::ios::end); m_endPos = m_stream.tellg(); m_stream.seekg(0, std::ios::beg); // read the snappy file identifier unsigned char byte1, byte2; m_stream >> byte1; m_stream >> byte2; assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2); flushReadCache(); } else if (m_stream.is_open() && mode == File::Write) {
void getstat(const std::string& pid,std::fstream& tsdbfile,std::string metric)//Get memmemory information (in one block right now) { std::string dummy,minflt,mjflt,utime,s_time,nthreads,vsize,rss,iodelay; char state; std::string path="/proc/"+pid+"/stat"; std::ifstream file(path,std::ifstream::binary); file>>dummy>>dummy>>state>>dummy>>dummy>>dummy>>dummy>>dummy>>dummy>>minflt>>dummy>>mjflt>>dummy>>utime>>s_time>>dummy>>dummy>>dummy>>dummy>>nthreads>>dummy>>dummy>>vsize>>rss; metric+=".stat"; std::string st_int; switch(state){ case 'R': st_int="1";break; case 'S': st_int="2"; break; case 'D': st_int="3"; break; case 'T': st_int="4"; break; default: st_int="0"; } tsdbfile.open ("tcollector_proc.out",std::fstream::app); tsdb_stdout(tsdbfile,metric+".state",st_int); tsdb_stdout(tsdbfile,metric+".minflt",minflt); tsdb_stdout(tsdbfile,metric+".mjrflt",mjflt); tsdb_stdout(tsdbfile,metric+".utime",utime); tsdb_stdout(tsdbfile,metric+".stime",s_time); tsdb_stdout(tsdbfile,metric+".nthreads",nthreads); tsdbfile.close(); return; }
void OpenUtf8FStreamForRead(std::fstream &aFStream, const char *aUtf8FileName) { // Unfortunately the windows C++ STL library will not open files specified via a UTF-8 filename. // The workaround is to convert the UTF-8 name to the short DOS compatible name and open that instead // First convert the UTF-8 name to UTF-16 for use with the windows APIs WCHAR *utf16Name = WindowsUtf8ToUtf16(aUtf8FileName); // Obtain short DOS compatible name for file WCHAR *utf16NameDos = WindowsRetrieveDosName(utf16Name); if(utf16NameDos == 0) { aFStream.setstate(std::ios_base::badbit); delete [] utf16Name; return; } char *utf8NameDos = WindowsUtf16ToUtf8(utf16NameDos); aFStream.open(utf8NameDos, std::ios_base::binary | std::ios_base::in); delete [] utf8NameDos; delete [] utf16NameDos; delete [] utf16Name; return; }
// Ensures the file is opened/closed properly and retries 5 times. // if choice is false, the file is closed and if it is 1, the file is opened. bool verifiedOC ( std::fstream& file, std::string fileDir, bool choice, std::ios::openmode io ) { unsigned int i = 0; // Declaring a counter variable. // Choice determines if we are opening or closing the file. (True to open, False to close) if ( choice ) { do { file.open ( fileDir.c_str(), io ); // Open file as user selection. if ( file.is_open() ) { return true; } else { // Prints that the attempt to change the file state has failed. std::cout << "The file " << fileDir.c_str() << " failed to open... Retrying " << ++i << "\n"; } // Will exit the loop after the the number of attempts FILE_OPEN_RETRIES specifies. if ( i >= FILE_OPEN_RETRIES ) { std::cout << "The file " << fileDir.c_str() << " failed to change open." << std::endl; return false; } } while ( !file.is_open() ); } else { file.close(); } return true; }
NMPRKC_API nmprk_status_t NMPRK_StartDebugLogging( const char *filename) { char dateStr[MAX_DATE_STR_LEN]; char timeStr[MAX_DATE_STR_LEN]; if(si_fsDebugLog.is_open() == true) return NMPRK_FAILURE; try { si_debugModule = SI_DEBUG_MODULE_ALL; si_debugLevel = SI_DEBUG_LEVEL_ALL; si_fsDebugLog.open(filename, std::fstream::out | std::fstream::app); if(si_fsDebugLog.is_open() != true) return NMPRK_FAILURE; } catch (...) { return NMPRK_FAILURE; } #if defined WIN32 _strdate_s(dateStr, MAX_DATE_STR_LEN); _strtime_s(timeStr, MAX_DATE_STR_LEN); #else time_t mytime = time(NULL); strftime(dateStr, 9, "%D", localtime(&mytime)); strftime(timeStr, 9, "%T", localtime(&mytime)); #endif SI_DEBUG_INFO(SI_THIS_MODULE, "Debug Logging Started: %s %s", dateStr, timeStr); return NMPRK_SUCCESS; }
int main(int argc, char *argv[]) { //Initialize symbol table code. Pin does not read symbols unless this is called PIN_InitSymbols(); //initialize Pin system if(PIN_Init(argc,argv)) { return Usage(); } string filename = KnobOutputFile.Value(); //file to record all instructions #ifdef LOG_ASSEM //TraceFile.open(filename, ios::out); AxOpenFile(TraceFile, filename); if (TraceFile.is_open()) { PRINT_SCN(filename << " : Start to make trace at instruction #" << InsCount); } else { PRINT_SCN("cannot open"); return -1; } #endif //file to record all memory accesses MemFile.open("mem.txt", ios::out); //add a function used to instrument at instruction granularity INS_AddInstrumentFunction(Instruction, 0); //call 'Fini' immediately before the application exits PIN_AddFiniFunction(Fini, 0); //starts executing the application PIN_StartProgram(); return 0; }
void open() { // // open file in out mode, close and open again in out/in mode // for (int ilayer=0; ilayer<4; ++ilayer) { vname[ilayer] << "temporary_file_v" << ilayer << "file-" << std::setfill('0') << std::setw(3) << angle << ".dat"; tname[ilayer] << "temporary_file_t" << ilayer << "file-" << std::setfill('0') << std::setw(3) << angle << ".dat"; uname[ilayer] << "temporary_file_u" << ilayer << "file-" << std::setfill('0') << std::setw(3) << angle << ".dat"; vfile[ilayer] = new fstream(vname[ilayer].str().c_str(), std::ios::binary | std::ios::out); vfile[ilayer]->close(); vfile[ilayer]->open(vname[ilayer].str().c_str(), std::ios::binary | std::ios::in | std::ios::out); tfile[ilayer] = new fstream(tname[ilayer].str().c_str(), std::ios::binary | std::ios::out); tfile[ilayer]->close(); tfile[ilayer]->open(tname[ilayer].str().c_str(), std::ios::binary | std::ios::in | std::ios::out); ufile[ilayer] = new fstream(uname[ilayer].str().c_str(), std::ios::binary | std::ios::out); ufile[ilayer]->close(); ufile[ilayer]->open(uname[ilayer].str().c_str(), std::ios::binary | std::ios::in | std::ios::out); } wname << "temporary_file_wfile-" << std::setfill('0') << std::setw(3) << angle << ".dat"; wfile = new fstream(wname.str().c_str(), std::ios::binary | std::ios::out); wfile->close(); wfile->open(wname.str().c_str(), std::ios::binary | std::ios::in | std::ios::out); }
void PcieAccessInterfaceTest::write_file(char* data, const std::string& path, uint32_t size, uint32_t offset) { memory_file.open(path, std::ios::out |std::ios::binary | std::ios::trunc); if (memory_file.is_open()) { memory_file.seekg(offset, std::ios::beg); memory_file.write(data, size); memory_file.close(); } }
void StoreCurrentFitnessInPopulation(Fitnesses& fitness, std::fstream& fitnessEachGeneration) { fitnessEachGeneration.open("FitnessEachGeneration.txt", std::fstream::in | std::fstream::out | std::fstream::app); int populationSize = fitness.size(); for(int i=0;i<populationSize;i++) { fitnessEachGeneration << fitness[i] <<'\n'; } fitnessEachGeneration.close(); }
void Request::open_batch_file(){ /* open batch_file */ batch_file_stream.open(batch_file, std::fstream::in); if( !batch_file_stream ){ perror_and_exit(("open batch_file " + batch_file + " error").c_str()); } is_batch_file_open = true; }
void load_terran() { terrain_stream.open("Data/Terrain.raw", std::ios::out | std::ios::binary | std::ios::in); if (terrain_stream.is_open()) { fprintf(stdout, "Terrain file open success\n"); } else { fprintf(stderr, "Terrain file open fail\n"); } terrain_stream.read((char*)terrain, MAP_SIZE * MAP_SIZE); }
/*! \brief Close and truncate the file, and update digest * Truncation is needed to remove any detritus from previously-saved states. */ void resultsfile::finishwithfile(std::fstream& file) { std::streampos length = file.tellp(); // close and truncate file file.close(); truncate(length); // re-open and update digest file.open(fname.c_str(), std::ios::in); filedigest.process(file); }
SeekableFileStream(const char *name) { _fileStream = new std::fstream(); _fileStream->open(name, std::fstream::in | std::fstream::binary); if (_fileStream->fail()) { _length = -1; } else { _fileStream->seekg(0, std::ios_base::end); _length = _fileStream->tellg(); _fileStream->seekg(0, std::ios_base::beg); } }
bool try_open(char *name, std::fstream &fin) { if(name == NULL){ return false; } fin.open(name, std::ios::in); if(fin.is_open()){ return true; } return false; }
/* void TechBot::Saveunknown_input() { std::fstream fout("unknown.txt", std::ios::out); if(fout.fail()) { throw std::string("Unable to save Unknown Input List"); } Vstr::const_iterator iter = ListOfUnknownInput.begin(); for( ; iter != ListOfUnknownInput.end(); ++iter ) { fout << *iter << std::endl; } fout.flush(); fout.close(); } */ void TechBot::Savelog() { time_t ltime; time(<ime); logfile.open("log.txt", std::ios::out | std::ios::app); if(logfile.fail()) { throw std::string("can't save conversation log"); } logfile << "\n\nConversation log - " << ctime(<ime) << std::endl; }
void open_file(std::fstream& ofs, std::string& filename, std::ios::openmode mode) { try { ofs.open( filename.c_str(), mode ); } catch (std::exception& ex) { std::string message = "Error opening file " + filename + ": " + ex.what(); throw std::runtime_error( message ); } }
void openLogFile(std::fstream& logFile, const char* logFileName) { // Open the log file with append flag logFile.open(logFileName, std::ios_base::app); if (logFile.fail()) { GET_TIME std::cout << timeBuffer << "\t" << SYS_CALL_ERR_MSG("open", strerror(errno)); exit(EXIT_FAILURE); } }
bool CFileManager::openFile(std::fstream &stream, const std::string &sFileName, std::ios_base::openmode mode, EStorageLocation eLocation) { assert(m_bInitialized); std::string path(getValidPath(sFileName, eLocation)); stream.open(path, mode); if (!stream) { Ogre::LogManager::getSingleton().logMessage(Ogre::LML_NORMAL, "Creating file at path: " + path); if (FILE *f = fopen(path.c_str(), "w+")) { fclose(f); stream.open(path, mode); } if (!stream) { Ogre::LogManager::getSingleton().logMessage(Ogre::LML_CRITICAL, "File " + path + " not found"); return false; } } if (stream) { Ogre::LogManager::getSingleton().logMessage(Ogre::LML_NORMAL, "File " + path + " openened"); } return true; }
Database::Database(char* file) { backing.open(file, std::ios::in|std::ios::out|std::ios::binary); if (backing.eof()) { char ver = FIDBVER; backing.write(&ver, 1); } else { char usedver = '\0'; backing.read(&usedver, 1); if (usedver != FIDBVER) { std::cerr << "Version " << usedver << " in file differs from " << FIDBVER << " in library!" << std::endl; return; } } indexstore = _ReadIndex(1); //Index is always on second byte }
/** * The initialisation function for the logger. Opens the file of name specified in argument. */ int ahns_logger_init(const char* filename) { ahns_logger_file.open(filename, std::ios::out | std::ios::app); if (ahns_logger_file.is_open()) { ahns_logger_file << "--------------------------------------------------------------------------------\n"; } else { throw std::runtime_error("AHNS LOG FILE OPEN FAILED"); } return 0; }
int User::writeUNPW(std::fstream &fout){ if (!fout.is_open()){ fout.open("UNPW.bin", std::ios::out, std::ios::binary); } auto fpos = fout.cur; fout.seekp(0, fout.beg); encode(username); encode(password); fout.write((char*)&username, sizeof(str)); fout.write((char*)&password, sizeof(str)); decode(username); decode(password); fout.seekp(0, fpos); return 0; }
void Game::init() { //Set up log file logFile.open("log.txt", std::fstream::out); //Make std::cerr redirect to the log file std::cerr.rdbuf(logFile.rdbuf()); logFile << "Initializing...\n"; SDL_Init(SDL_INIT_EVERYTHING); if((window = SDL_CreateWindow(baseWindowTitle.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenHeight, SDL_WINDOW_OPENGL)) == 0) { logFile << "Error creating window: " << SDL_GetError() << std::endl; stop(); return; } logFile << "Created window" << std::endl; if((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL) { logFile << "Error creating renderer: " << SDL_GetError() << std::endl; stop(); return; } logFile << "Created renderer" << std::endl; if((glcontext = SDL_GL_CreateContext(window)) == NULL) { logFile << "Error creating OpenGL context: " << SDL_GetError() << std::endl; stop(); return; } glewInit(); logFile << "Created OpenGL context" << std::endl; //Initialize OpenGL SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); resizeWindow(screenWidth, screenHeight); lastMouseState = 0; //Initialize things here curScene = new Scene(); // logFile << "Finished initializing" << std::endl; }
exp bool Open(const char* fname, unsigned int pO, unsigned int pLen) { partitionOffset = pO; partitionLength = pLen; // Open the image f.open(fname, std::ios::in | std::ios::out | std::ios::binary); if (!f.is_open()) { LastError("Open", "Failed to open disk"); return false; } f.seekg(0, std::ios::end); fsize = (size_t)f.tellg(); f.seekg(0); // Allocate a buffer sectorBuffer = new char[SECTOR_SIZE]; // Read the BPB if (!ReadBPB()) { LastError("Open", "Failed to read the BPB"); return false; } // Read filesystem info fsInfo = new FileSystemInfo; ReadSector((char*)fsInfo, bpb->clusterFSInfo); // Load the root directory if (!DirOpenRoot()) { LastError("Open", "Failed to load the root directory"); return false; } volumeId = new char[DOS83_MAX_LEN + 1]; ReadSector(sectorBuffer, ClusterToSector(bpb->clusterRoot)); DirectoryEntry* entry = FindEntryAttribute(ATTRIB_VOLUME_ID, &directory); if (entry) { memcpy(volumeId, entry->name, DOS83_MAX_LEN); volumeId[11] = 0; } return true; }
bool OpenFile(const std::wstring& aFile, std::fstream& aStream, std::ios_base::open_mode aMode) { std::string s; aStream.open(Ucs2ToUtf8(aFile, s).c_str(), aMode); return aStream.good(); }