NeoPZ
TPZFileStream.cpp
Go to the documentation of this file.
1 #include "TPZFileStream.h"
2 #include "pzerror.h"
3 
4 #ifdef _AUTODIFF
5 #include "fad.h"
6 #include "tfad.h"
7 #endif
8 
10 }
11 
13  CloseWrite();
14  CloseRead();
15 }
16 
17 void TPZFileStream::OpenRead(const std::string &fileName) {
18 #ifdef PZDEBUG
19  if (AmIOpenForRead()) {
20  PZError<<"TPZFileStream: File is already opened"<<std::endl;
21  }
22 #endif
23  fIn.open(fileName.c_str());
24 #ifdef PZDEBUG
25  if (!AmIOpenForRead()) {
26  PZError<<"TPZFileStream: Could not open file"<<std::endl;
27  }
28 #endif
29 }
30 
31 void TPZFileStream::OpenWrite(const std::string &fileName) {
32 #ifdef PZDEBUG
33  if (AmIOpenForWrite()) {
34  PZError<<"TPZFileStream: File is already opened"<<std::endl;
35  }
36 #endif
37  fOut.open(fileName.c_str(), std::ofstream::binary | std::ofstream::trunc);
38 #ifdef PZDEBUG
39  if (!AmIOpenForWrite()) {
40  PZError<<"TPZFileStream: Could not open file"<<std::endl;
41  }
42 #endif
43 }
44 
46  return fIn.is_open();
47 }
48 
50  return fOut.is_open();
51 }
52 
54  if (fIn.is_open()) {
55  fIn.close();
56  }
57 }
58 
60  if (fOut.is_open()) {
61  fOut.close();
62  }
63 }
64 
65 template<class T>
66  void TPZFileStream::ReadData(T *p, int howMany) {
67  int c;
68  char buf[100];
69  if(!fIn)
70  {
71  DebugStop();
72  }
73  if(howMany)
74  {
75  for(c=0; c<howMany; c++) fIn >> p[c];
76  fIn.getline(buf,100);
77  }
78 #ifdef PZDEBUG
79  if (fIn.bad()) {
80  PZError << "TFileStream:Could not read from stream" << std::endl;
81  DebugStop();
82  }
83 #endif
84  }
85 
86 template<class T>
87  void TPZFileStream::WriteData(const T *p, int howMany){
88  for(int c=0; c<howMany; c++) fOut << p[c] << std::endl;
89 #ifdef PZDEBUG
90  if (fOut.bad()) {
91  PZError << "TFileStream:Could not write to stream" << std::endl;
92  DebugStop();
93  }
94 #endif
95  }
void ReadData(T *p, int howMany)
virtual ~TPZFileStream()
void OpenWrite(const std::string &fileName)
Defines PZError.
void OpenRead(const std::string &fileName)
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
std::ifstream fIn
Definition: TPZFileStream.h:17
std::ofstream fOut
Definition: TPZFileStream.h:18
void WriteData(const T *p, int howMany)
virtual bool AmIOpenForWrite()
virtual bool AmIOpenForRead()
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15