Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:30

0001 /*
0002  * FileIO.cc
0003  *
0004  *  Created on: Sep 25, 2012
0005  *      Author: aspataru
0006  */
0007 
0008 #include "EventFilter/Utilities/interface/FileIO.h"
0009 
0010 #include <iostream>
0011 #include <fstream>
0012 #include <streambuf>
0013 #include <cstdlib>
0014 #include <dirent.h>
0015 #include <cstdio>
0016 #include <sys/stat.h>
0017 #include <cstring>
0018 
0019 using namespace jsoncollector;
0020 
0021 void FileIO::writeStringToFile(std::string const& filename, std::string& content) {
0022   std::ofstream outputFile;
0023   outputFile.open(filename.c_str());
0024   outputFile << content;
0025   outputFile.close();
0026 }
0027 
0028 bool FileIO::readStringFromFile(std::string const& filename, std::string& content) {
0029   if (!fileExists(filename))
0030     return false;
0031 
0032   std::ifstream inputFile(filename.c_str());
0033   inputFile.seekg(0, std::ios::end);
0034   content.reserve(inputFile.tellg());
0035   inputFile.seekg(0, std::ios::beg);
0036   content.assign((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
0037   inputFile.close();
0038   return true;
0039 }
0040 
0041 bool FileIO::fileExists(std::string const& path) {
0042   std::ifstream ifile(path.c_str());
0043   return !ifile.fail();
0044 }