41 #define SAFE_FOPEN(pfile, path, mode) {\ 42 if ( !path || !(pfile = fopen(path, mode)) ) {cout<<"File Open Failed: "<<path<<endl; exit(0);}} 43 #define SAFE_FMOPEN(file, path, mode) {\ 45 file.open(path, mode); if (!file.good()) {cout<<"File Open Failed: "<<path<<endl; exit(0);}} 46 #define SAFE_FSOPEN(file, path) {\ 48 file.open(path); if (!file.good()) {cout<<"File Open Failed: "<<path<<endl; exit(0);}} 49 #define SAFE_FCLOSE(fp) { if(fp) {fclose(fp); fp=NULL;} } 53 #define MAX_SENTENCE_LEN 32*1024 56 #define WRITE_VALUE(x,fp) fwrite(&(x),sizeof(x),1,fp) 57 #define READ_VALUE(x,fp) fread(&(x),sizeof(x),1,fp) 59 #define FPRINT_VALUE(x, fp) fprintf(fp, #x"=%d\n", (x)) 61 #define FSCANF_VALUE(x, fp) fscanf(fp, #x"=%d\n", &(x)) 108 File() :fp(NULL), pStrLine(NULL) { fp = NULL; bOver =
false; nBuf = 1; }
110 File(
const char *path,
const char *mode,
bool bHardOpen =
true) :fp(NULL), pStrLine(NULL) { Open(path, mode, bHardOpen); }
120 virtual bool Open(
const char *path,
const char *mode,
bool bHardOpen =
true);
122 virtual bool Reopen(
const char* model);
126 operator FILE* () {
return fp; }
132 virtual char *GetLine(
bool bPrecent =
false);
136 virtual void Print(
const char* p_pMessage, ...);
138 virtual int Scanf(
const char* p_pMessage, ...);
143 void Reset() { _fseeki64(fp, 0, SEEK_SET); nLine = 0; }
145 bool Good()
const {
return !(fp == NULL); }
147 template <
typename TYPE>
150 for (
int i = 0; i<num; i++) {
151 Print(pformat, *pbuf);
184 ObjFile(
const char *path,
const char *mode) :
File(path, mode), m_nTotalNum(0), m_nCurNum(-1){}
int m_nTotalNum
object number
short nBuf
record the buffer is nBuf times of GS_SENTENCE_LEN
virtual void ReadT(File &file)
read from the txt file
void Reset()
reset position
virtual void Close()
close the file
#define SAFE_FCLOSE(fp)
file close
virtual void WriteB(File &file)
write to binary file
base class used to derive. It providing the vritual function of write and read
virtual void ReadB(File &file)
read from binary file
a definition of a class Log, which can output to the cmd window and the log file simultaneously. In wb-log.cpp, there are a Log variable "lout", which can be directly used just like "cout". For example:
Provide the toolkits for cmd window of window platform.
int m_nCurNum
current object number
ObjFile(const char *path, const char *mode)
constructor
string strFileName
stroe the file name
used to read more than one objects
IO_Obj * m_pObj
object pointer
bool bOver
record the if the buffer is overflow
char * pStrLine
store the string get from file
bool Good() const
return if the file is accessible.
void PrintArray(const char *pformat, TYPE *pbuf, int num)
print a array into file
int nLine
the number of reading from file
#define MAX_SENTENCE_LEN
the maximum length of string read from file once
File(const char *path, const char *mode, bool bHardOpen=true)
constructor
define all the code written by Bin Wang.
virtual void WriteT(File &file)
write to the txt file