本文整理汇总了C++中std::fstream::open方法的典型用法代码示例。如果您正苦于以下问题:C++ fstream::open方法的具体用法?C++ fstream::open怎么用?C++ fstream::open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::fstream
的用法示例。
在下文中一共展示了fstream::open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getFILE
// 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;
}
示例2: createAndOpen
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);
}
示例3: SetUp
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();
}
示例4: createWalletFile
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);
}
示例5: OpenFStream
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
}
示例6: main
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;
}
示例7: open
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);
}
示例8: OpenFile
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();
}
示例9: switch
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();
}
示例10: rawOpen
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) {
示例11: file
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;
}
示例12: OpenUtf8FStreamForRead
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;
}
示例13: NMPRK_StartDebugLogging
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;
}
示例14: verifiedOC
// 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;
}
示例15: write_file
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();
}
}