📄 mfile.cpp
字号:
// $common\mfile.cpp 1.5 milbo$ some file handling routines// Warning: this is raw research code -- expect it to be quite messy.// milbo durban dec05#include <windows.h>#include <io.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <signal.h>#include <float.h>#include <math.h>#include <new.h>#include <direct.h>#include <sys/stat.h>#include <fstream.h>#include <iostream.h>#include "mcommon.hpp"#include "util.hpp"static char sgPrintBuf[CONF_nMaxPrintfLen];//-----------------------------------------------------------------------------// Like fopen but prints a message and exits if open failsFILE *Fopen (const char *sPath, const char *sMode){FILE *pFile = fopen(sPath, sMode);if (!pFile) { char *s; switch (sMode[0]) { case 'w': s = " for writing"; break; case 'a': s = " for appending"; break; case 'r': s = ""; break; default: s = " (unknown mode)"; break; } Err("Can't open %s%s", sPath, s); }return pFile;}//-----------------------------------------------------------------------------// Like fwrite but prints a message and exits if write failsvoid Fwrite (const void *buf, size_t Size, size_t Count, FILE *pFile, char *sPath){if (fwrite(buf, Size, Count, pFile) != Count) Err("Can't write %s", sPath); // will exit the program}//-----------------------------------------------------------------------------// Like fprintf but prints a message and exits if fprintf failsvoid __cdecl Fprintf (FILE *pFile, const char *pArgs, ...){va_list pArg;va_start(pArg, pArgs);vsprintf(sgPrintBuf, pArgs, pArg);va_end(pArg);int Len = strlen(sgPrintBuf);DASSERT(Len < CONF_nMaxPrintfLen);if (Len != fprintf(pFile, "%s", sgPrintBuf)) Err("Can't write to file: %s", sgPrintBuf);}//-----------------------------------------------------------------------------// Like Fprintf but has a filename parameter for better error reportingvoid __cdecl Fprintf (const char *sFile, FILE *pFile, const char *pArgs, ...){va_list pArg;va_start(pArg, pArgs);vsprintf(sgPrintBuf, pArgs, pArg);va_end(pArg);int Len = strlen(sgPrintBuf);DASSERT(Len < CONF_nMaxPrintfLen-1);if (Len != fprintf(pFile, "%s", sgPrintBuf)) Err("Can't write to %s: %s", sFile, sgPrintBuf);}//-----------------------------------------------------------------------------// Like fread but prints a message and exits if read failsvoid Fread (void *buf, size_t Size, size_t Count, FILE *pFile, const char *sPath){if (fread(buf, Size, Count, pFile) != Count) Err("Can't read %s", sPath); // will exit the program}//-----------------------------------------------------------------------------// Like gets but skips comments ie white space lines and lines beginning with #char *Fgets (char *s, int n, FILE *stream){char *p;bool fComment;do { p = fgets(s, n, stream); fComment = (p != NULL) && (s[0] == '#' || strspn(s, " \t\n") == strlen(s)); }while (fComment);return p;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -