Path Integral Quantum Monte Carlo
Public Member Functions | Protected Member Functions | Protected Attributes | Friends
File Class Reference

A basic input/output file class. More...

#include <communicator.h>

Public Member Functions

 File (string, string, string, string)
 Constructor. More...
 
 File (string)
 Constructor. More...
 
fstream & stream ()
 
void open (ios_base::openmode)
 Open the file. More...
 
void reset ()
 Reset a file. More...
 
void rename ()
 Rename a file. More...
 
void close ()
 Close the file.
 
bool exists ()
 did the file exist before opening?
 

Protected Member Functions

void open (ios_base::openmode, string)
 Open the file. More...
 

Protected Attributes

string name
 
string bakname
 
bool exists_
 
fstream rwfile
 

Friends

class Communicator
 

Detailed Description

A basic input/output file class.

Definition at line 25 of file communicator.h.

Constructor & Destructor Documentation

◆ File() [1/2]

File::File ( string  _type,
string  _data,
string  ensemble,
string  outDir 
)

Constructor.

Given a file type/label, ensemble type, data label and out directory, create a unique file name and backup file name.

Parameters
_typeThe file type: state, log, etc
_dataThe unique data string identifier
ensemblece: canonical, gce: grand canonical
outDirThe output directory

Definition at line 28 of file communicator.cpp.

28  {
29 
30  /* The file name */
31  name = str(format("%s/%s-%s-%s.dat") % outDir % ensemble % _type % _data);
32 
33  /* Create a backup name */
34  bakname = str(format("%s/%s-%s-%s.bak") % outDir % ensemble % _type % _data);
35 
36  /* Determine if the file already exists */
37  exists_ = filesystem::exists(name);
38 }
ensemble
Each move can operate on only the digaonal ensemble, only the off-diagonal ensemble,...
Definition: common.h:133

◆ File() [2/2]

File::File ( string  _name)

Constructor.

Create a filename from a string.

Parameters
_nameA file name.

Definition at line 47 of file communicator.cpp.

47  : name(_name), bakname() {
48 
49 }

Member Function Documentation

◆ open() [1/2]

void File::open ( ios_base::openmode  mode)

Open the file.

Parameters
modeA valid file rw mode

Definition at line 64 of file communicator.cpp.

64  {
65 
66  /* Convert the filename to a c string, and open the file */
67  rwfile.open(name.c_str(), mode);
68  if (!rwfile) {
69  cerr << "Unable to process file: " << name << endl;
70  exit(EXIT_FAILURE);
71  }
72 }
+ Here is the caller graph for this function:

◆ open() [2/2]

void File::open ( ios_base::openmode  mode,
string  _name 
)
protected

Open the file.

Parameters
modeA valid file rw mode
_nameA valid file name

Definition at line 80 of file communicator.cpp.

80  {
81 
82  /* Convert the filename to a c string, and open the file */
83  rwfile.open(_name.c_str(), mode);
84  if (!rwfile) {
85  cerr << "Unable to process file: " << _name << endl;
86  exit(EXIT_FAILURE);
87  }
88 }

◆ rename()

void File::rename ( )

Rename a file.

After we have performed a write to a .bak file, we rename it to .dat

Definition at line 112 of file communicator.cpp.

112  {
113 
114  close();
115 
116  /* /1* Create the filesystem paths *1/ */
117  /* boost::filesystem::path datpath(name); */
118  /* boost::filesystem::path bakpath(bakname); */
119 
120  /* Perform the rename */
121  filesystem::rename(bakname.c_str(), name.c_str());
122 }
void close()
Close the file.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reset()

void File::reset ( )

Reset a file.

This method is used to prepare a file for writing that is meant to be overwritten. In order to be safe, we write to a .bak version of the file.

Definition at line 96 of file communicator.cpp.

96  {
97 
98  /* Close the current file */
99  close();
100 
101  /* Open a backup file and replace any content if it exists */
102  open(ios::out|ios::trunc,bakname);
103 
104  /* Write the generic header to the file */
105 }
void open(ios_base::openmode)
Open the file.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: