This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  File Handling Class
  Submitted by



This code describes a C++ class for file handling. This is how you use the class cFile:
cFile file; //this creates a filehandling object of the class cFile
file.OpenRW("filename"); //this opens a file with read and write access
(i.e. you can both read and write to the file, "filename" is the name of
the file to open
int id;
file.Read(id); //this reads the number of bytes contained in id from the
file and puts them in id
file.Write(id); //this writes the entire contents of id to the file
int* data;
file.Read(data, sizeof(int)*4); //if the variable you want to read or
write is a pointer and you want the memory space that the pointer points
to to be read/written to then you NEED to specify the number of bytes to
read/write
file.Write(data, sizeof(int)*4);
file.Close(); //this closes the file 



FURTHER functionality
Here are some other useful functions in the cFile class: file.OpenROM(char*) //use this to open a file for "Read Only" access, this does not allow you to write to the file
file.OpenWOM(char*) //use this to open a file for "Write Only" access, this does not allow you to read from the file
use cFile::IsOpen() to determin if a file is open, if opening a file failed then the opening function returned false and this function will also return false. Note that this function does not return false if you tried to open a file when already another file was opened with the same cFile object. This function only tells you if the current cFile object describes a file that you can close and read from and or write to. (All the occurances of WOM in this code have something to do with "Write Only Memory", all occurances of ROM stand for "Read Only Memory" and RW stands for "Read/Write".) You may open several files with a single object of cFile but you have to close the currently open file with cFile::Close before you open another one. This introduction should be all you need to open a file and read and write to it, if you want to do more you could look in the code, I hope you'll understand what the functions are for and how to use them. If not you can contact me at jesper@atrad.se.


Download Associated File: File.h (4,531 bytes)

/*
++	File name: File.h
++	File comments: This class is used for file handling.
		You may copy, reproduce and redistribute this code
		with the condition that you include this copyright
		notice and don't recieve payment in any kind for this
		code in original or modified form.
++	Copyright (c) 2002, Jesper Öqvist
*/

//created: 2002-0?-?? //last updated: 2002-11-05 #ifndef _JSH_FILE_H #define _JSH_FILE_H

//includes: #include <windows.h>

//Use these macros to specify what kind of access you need #define FILE_ROM GENERIC_READ #define FILE_WOM GENERIC_WRITE #define FILE_RW (GENERIC_READ | GENERIC_WRITE)

//Use these macros to specify how the file will be created/opened #define FILE_CREATE_NEW CREATE_NEW #define FILE_CREATE_ALWAYS CREATE_ALWAYS #define FILE_OPEN_EXISTING OPEN_EXISTING #define FILE_OPEN_ALWAYS OPEN_ALWAYS

//Various file attributes. #define FILE_ATTRB_NORMAL FILE_ATTRIBUTE_NORMAL #define FILE_ATTRB_HIDDEN FILE_ATTRIBUTE_HIDDEN #define FILE_ATTRB_ROM FILE_ATTRIBUTE_READONLY //FILE_ATTRIBUTE_NORMAL //FILE_ATTRIBUTE_HIDDEN //FILE_ATTRIBUTE_READONLY //These are used to specify if other programs will be able // to access the file at the same time as you are. //FILE_SHARE_READ //FILE_SHARE_WRITE #define FILE_SHARE_RNW (FILE_SHARE_READ | FILE_SHARE_WRITE)

//These are used when moving the file pointer. //FILE_CURRENT //FILE_BEGIN //FILE_BEGIN //FILE_END class cFile { public: DWORD m_rw;

private: HANDLE m_fh; char* m_fname; bool m_write; bool m_read;

public: cFile() {m_fh = m_fname = NULL;m_write = m_read = false;} cFile(cFile& of){ DuplicateHandle( GetCurrentProcess(), of.m_fh, GetCurrentProcess(), &m_fh, 0, false, DUPLICATE_SAME_ACCESS); m_write = of.m_write;m_read = of.m_read;}; ~cFile() {if (m_fh) CloseHandle(m_fh);}

bool OpenRW(char* filename){ return Open(filename, FILE_RW, 0, FILE_OPEN_ALWAYS);}

bool OpenROM(char* filename){ return Open(filename, FILE_ROM, FILE_SHARE_READ, FILE_OPEN_EXISTING);}

bool OpenWOM(char* filename){ return Open(filename, FILE_WOM, 0, FILE_CREATE_ALWAYS);}

bool Open(char* filename, DWORD access, DWORD sharemode, DWORD openmode, DWORD attributes = 0) { if (m_fh) return false; m_fname = filename; m_fh = CreateFile(m_fname, access, sharemode, NULL, openmode, attributes,NULL); if (m_fh == INVALID_HANDLE_VALUE) return false; if (access & FILE_RW) { m_write = m_read = true; } else if (access & FILE_ROM) { m_read = true; m_write = false; } else { m_read = false; m_write = true; } return true; }

template <class T> bool Write(T& data) {return Write(&data, sizeof(data));} bool Write(const void* data, DWORD len, DWORD* bwritten = NULL) { if (m_fh && m_write) { if (bwritten) { if (!WriteFile(m_fh, data, len, bwritten, NULL)) return false; } else { if (!WriteFile(m_fh, data, len, &m_rw, NULL)) return false; } return true; } return false; }

template <class T> bool Read(T& data) {return Read(&data, sizeof(data));} bool Read(void* data, DWORD len, DWORD* bread = NULL) { if (m_fh && m_read) { if (bread) { if (!ReadFile(m_fh, data, len, bread, NULL)) return false; } else { if (!ReadFile(m_fh, data, len, &m_rw, NULL)) return false; } return true; } return false; }

void Close() {if (m_fh) CloseHandle(m_fh);m_fh = NULL;}

HANDLE GetHandle() {return m_fh;} HANDLE Handle() {return m_fh;} void SetHandle(HANDLE handle) {m_fh = handle;} void Handle(HANDLE handle) {m_fh = handle;} DWORD SetPointer(DWORD moveto, DWORD movemethod){ return SetFilePointer(m_fh, moveto, NULL, movemethod);} DWORD Pointer(DWORD moveto, DWORD movemethod){ return SetFilePointer(m_fh, moveto, NULL, movemethod);} DWORD GetPointer(){ return SetFilePointer(m_fh, 0, NULL, FILE_CURRENT);} DWORD Pointer(){ return SetFilePointer(m_fh, 0, NULL, FILE_CURRENT);} DWORD GetSize() {return GetFileSize(m_fh, NULL);} DWORD Size() {return GetFileSize(m_fh, NULL);}

cFile& operator=(const cFile& of){ DuplicateHandle( GetCurrentProcess(), of.m_fh, GetCurrentProcess(), &m_fh, 0, false, DUPLICATE_SAME_ACCESS); m_write = of.m_write;m_read = of.m_read; return *this;};

bool IsOpen() {return (m_fh == INVALID_HANDLE_VALUE) ? false : true;} };

#endif //_JSH_FILE_H

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.