Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- cFile - a revolutionary file read/write system coded by Fami Harukaze!
- 04.08.2014
- */
- #ifndef __cFile
- #define __cFile
- #include <sys/stat.h>
- #include <stdio.h>
- #include <io.h>
- #include <minwindef.h>
- #include <fileapi.h>
- #include <stdarg.h>
- class cFile
- {
- public:
- static bool __stdcall Exists(char*);
- static void __stdcall Write(char*, char*, ...);
- static void __stdcall Append(char*, char*, ...);
- static char* __stdcall Read(char*);
- public:
- static _iobuf* __stdcall cfopen(const char*, const char*);
- };
- _iobuf* __stdcall cFile::cfopen(const char* fn, const char* m)
- {
- if (_access(fn, 0) == -1)
- {
- char p[MAX_PATH];
- strcpy(p, fn);
- for (unsigned int i = 0; i < strlen(p); i++)
- if (p[i] == '/')
- {
- char oldch = p[i];
- p[i] = 0;
- CreateDirectoryA(p, NULL);
- p[i] = oldch;
- }
- }
- return fopen(fn, m);
- } // Credits: NanoCat
- bool __stdcall cFile::Exists(char* name)
- {
- struct stat buffer;
- return (stat(name, &buffer) == 0);
- }
- void __stdcall cFile::Append(char* strFilename, char* strContent, ...)
- {
- char szBuffer[16864] = { 0 };
- va_list va;
- va_start(va, strContent);
- _vsnprintf(szBuffer, sizeof(szBuffer), strContent, va);
- va_end(va);
- _iobuf* filelog = cfopen(strFilename, "a");
- fprintf(filelog, "%s", szBuffer);
- fclose(filelog);
- }
- void __stdcall cFile::Write(char* strFilename, char* strContent, ...)
- {
- char szBuffer[16864] = { 0 };
- va_list va;
- va_start(va, strContent);
- _vsnprintf(szBuffer, sizeof(szBuffer), strContent, va);
- va_end(va);
- _iobuf* filelog = cfopen(strFilename, "wb");
- fprintf(filelog, "%s", szBuffer);
- fclose(filelog);
- }
- char* __stdcall cFile::Read(char* strFilename)
- {
- char* strFilecontent;
- strFilecontent = "";
- if (Exists(strFilename))
- {
- FILE* fileStream;
- fileStream = cfopen(strFilename, "r");
- if (!fileStream) {
- return "";
- }
- // Determine file size
- fseek(fileStream, 0, SEEK_END);
- size_t size = ftell(fileStream);
- char* where = new char[size];
- rewind(fileStream);
- fread(where, sizeof(char), size, fileStream);
- strFilecontent = where;
- delete[] where;
- }
- return strFilecontent;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement