📄 file.h
字号:
/*------------------------------------------------------------------------------*
* File Name: File.h *
* Creation: 4/2/2001 *
* Purpose: Origin C header for File class and other related functions *
* Copyright (c) OriginLab Corp.2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 *
* All Rights Reserved *
* *
* Modification Log: *
*------------------------------------------------------------------------------*/
#ifndef _FILE_H
#define _FILE_H
#include <common.h> // must always include this
#include <string.h> // most likely you will also need strings
//constants for file nOpenFlags
//CPY, we will change into enum later
/*
enum {
modeRead = 0x0000,
modeWrite = 0x0001,
modeReadWrite = 0x0002,
shareCompat = 0x0000,
shareExclusive = 0x0010,
shareDenyWrite = 0x0020,
shareDenyRead = 0x0030,
shareDenyNone = 0x0040,
modeNoInherit = 0x0080,
modeCreate = 0x1000,
modeNoTruncate = 0x2000,
typeText = 0x4000, // typeText and typeBinary are used in
typeBinary = (int)0x8000 // derived classes only
};
*/
#define file::modeRead 0x0000
#define file::modeWrite 0x0001
#define file::modeReadWrite 0x0002
#define file::shareCompat 0x0000
#define file::shareExclusive 0x0010
#define file::shareDenyWrite 0x0020
#define file::shareDenyRead 0x0030
#define file::shareDenyNone 0x0040
#define file::modeNoInherit 0x0080
#define file::modeCreate 0x1000
#define file::modeNoTruncate 0x2000
#define file::typeText 0x4000 // typeText and typeBinary are used in
#define file::typeBinary ((int)0x8000) // derived classes only
#define file::hFileNull 0
#define file::begin 0
#define file::current 1
#define file::end 2
//constants for file Error Code
#define FileError::none 0 //No error occurred.
#define FileError::generic 1 //An unspecified error occurred.
#define FileError::fileNotFound 2 //The file could not be located.
#define FileError::badPath 3 //All or part of the path is invalid.
#define FileError::tooManyOpenFiles 4 //The permitted number of open files was exceeded.
#define FileError::accessDenied 5 //The file could not be accessed.
#define FileError::invalidFile 6 //There was an attempt to use an invalid file handle.
#define FileError::removeCurrentDir 7 //The current working directory cannot be removed.
#define FileError::directoryFull 8 //There are no more directory entries.
#define FileError::badSeek 9 //There was an error trying to set the file pointer.
#define FileError::hardIO 10 //There was a hardware error.
#define FileError::sharingViolation 11 //SHARE.EXE was not loaded, or a shared region was locked.
#define FileError::lockViolation 12 //There was an attempt to lock a region that was already locked.
#define FileError::diskFull 13 //The disk is full.
#define FileError::endOfFile 14 //The end of file was reached.
/** >System
The Origin C file class provides read/write access to binary files using unbuffered io
(immediate disk access) similar to the MFC CFile class. See also the stdioFile class
for buffered stream io to text files.
Example:
char szTemp[MAXFULLPATH];
GetTempPath(MAXFULLPATH, szTemp); // Get temp folder path.
string strFile = szTemp + "\\test.Dat";
file ff(strFile, file::modeCreate | file::modeWrite); // Create a new write only file.
int bb[4] = {1, 2, 3, 4};
int iSize = sizeof(bb);
ff.Write(bb,iSize);
ff.Close();
*/
class file
{
public:
/**
Remarks:
The default constructor does not open a file but rather sets m_hFile to file::hFileNull.
Example:
file ff;
SeeAlso:
file::Open,file::Close.
*/
file(); // Set the public data member m_hFile to a null value.
/**
Remarks:
Creates a file object and opens the file having the path specified by the parameter lpszFileName. This
constructor combines the functions of the default constructor file() and the Open member function. An exception
is thrown if an error occurs while opening the file. Depending on the severity of the error, the user should be
alerted by an exception handler.
Example:
char szTemp[MAXFULLPATH];
GetTempPath(MAXFULLPATH, szTemp); // Get temp folder path.
string strFile = szTemp + "\\test.txt";
file ff(strFile, file::modeCreate | file::modeWrite); // Create a new write only file.
int bb[4] = {1, 2, 3, 4};
ff.Write(bb, sizeof(bb));
ff.Close();
Parameters:
lpszFileName=A string containing an absolute or relative path to the desired file.
nOpenFlags=Specifies the share and access modes of the opened file. Modes are defined below and can be combined
using the bitwise OR operator "|". At least one access and one share mode should be specified.
modeRead Opens the file in read only mode.
modeWrite Opens the file in write only mode.
modeReadWrite Opens the file in read and write modes.
shareCompat Not available...see shareExclusive below.
shareExclusive Opens the file disallowing read and write access to all other processes. Construction fails
if file is already opened in read or write mode.
shareDenyWrite Opens the file disallowing write access to all other processes. Construction fails if file
is already opened in write mode.
shareDenyRead Opens the file disallowing read access to all other processes. Construction fails if file
is already opened in read mode.
shareDenyNone Opens the file allowing read and write access to all other processes.
modeNoInherit Opens the file but disallows inheritance by child processes.
modeCreate Creates a new file of length 0 bytes. Pre-existing files are truncated to 0 bytes by default.
modeNoTruncate Used in combination with modeCreate to specify that pre-existing files should not be truncated.
When used together, modeCreate and modeNoTruncate open a specified file if it exists or create
the specified file if it does not exist.
typeText Sets text mode for specified file enabling special processing of carriage return-line feed
character combinations. Used in derived classes only.
typeBinary Sets binary mode for specified file. Used in derived classes only.
SeeAlso:
file::Open,file::Close.
*/
file(LPCTSTR lpszFileName, UINT nOpenFlags); // Create a File object and open the file having the specified path.
// Attributes
/**
Remarks:
Get the value of the last error that occured.
FileError::none = No error occurred.
FileError::generic = An unspecified error occurred.
FileError::fileNotFound = The file could not be located.
FileError::badPath = All or part of the path is invalid.
FileError::tooManyOpenFiles = The permitted number of open files was exceeded.
FileError::accessDenied = The file could not be accessed.
FileError::invalidFile = There was an attempt to use an invalid file handle.
FileError::removeCurrentDir = The current working directory cannot be removed.
FileError::directoryFull = There are no more directory entries.
FileError::badSeek = There was an error trying to set the file pointer.
FileError::hardIO = There was a hardware error.
FileError::sharingViolation = SHARE.EXE was not loaded, or a shared region was locked.
FileError::lockViolation = There was an attempt to lock a region that was already locked.
FileError::diskFull = The disk is full.
FileError::endOfFile = The end of file was reached.
Example:
This function open/create the file c:\test.txt and get the value of the last error
if open file fails.
void run_GetLastError()
{
file ff;
string strFile = "C:\\test.txt";
string FileErr;
BOOL bOK;
bOK = ff.Open(strFile, file::modeCreate | file::modeWrite); //Create a write only file and open it
if (!bOK)
{
printf("failed to open the file"); //the string should not be output
FileErr = ff.GetLastError();
}
else
{
ff.Close();
}
}
*/
UINT GetLastError();
/**
Remarks:
Check file object is open or not, when use a method of File object which is not open will lead a runtime error.
Use this function before you call the method of File object.
Example:
This function opens/creates the file c:\test.txt.
void run_IsOpen()
{
file ff;
BOOL bOK;
string strFile = "C:\\test.txt";
bOK = ff.Open(strFile, file::modeCreate | file::modeWrite); //create a new write only file and open it
if(!bOK)
out_str("failed to open the file");
else
{
if( ff.IsOpen() )
{
printf("The ff file is open"); //The string should be output
ff.Close(); //Close the ff file
}
}
}
*/
BOOL IsOpen();
/**
Remarks:
Get the current value of the file pointer which is an offset from the beginning of the file.
Example:
char szTemp[MAXFULLPATH];
GetTempPath(MAXFULLPATH, szTemp); // Get temp folder path.
string strFile = szTemp + "\\test.txt";
file ff;
if( ff.Open(strFile, file::modeCreate | file::modeWrite) )
{
int i = 10;
int ia[4] = {1, 2, 3, 4};
LONG lPosition;
ff.Write(&i, sizeof(i)); // Write 1 int (4 bytes) to file.
lPosition = ff.GetPosition(); // Position now equals 4.
ff.Write(ia, sizeof(ia)); // Write 4 ints (16 bytes) to file following previous int.
lPosition = ff.GetPosition(); // Position now equals 20.
ff.SeekToBegin(); // Set file pointer to start of file.
lPosition = ff.GetPosition(); // Position now equals 0.
ff.Close();
}
Return:
Returns the current value of the file pointer.
SeeAlso:
file::Seek.
*/
LONG GetPosition();
/**
Remarks:
A safe method of opening a file when failure is a likely outcome. The Open member function should be called after
the default File constructor has been invoked. It is a safe method of opening a file because it returns FALSE
or 0 on failure instead of throwing an exception. When a file is opened the current pointer position is
initialized to 0.
Example 1:
file ff;
//Get the origin folder path.
string strOriginPath;
strOriginPath = GetAppPath() + "OriginC";
string strFile = strOriginPath + "\\test.txt";
BOOL bOK = ff.Open(strFile, file::modeCreate | file::modeWrite); //create a new write only file.
int bb[4] = {1, 2, 3, 4};
ff.Write(bb, sizeof(bb));
ff.Close();
bOK = ff.Open(strFile, file::modeRead | file::shareDenyWrite); //open the file created previously and set it to read only file.
int aa;
ff.Read(&aa, sizeof(aa)); //aa equals 1 now.
ff.Close();
Example 2:
//Get temp folder path.
char sztemp[MAXFULLPATH];
DWORD aa = GetTempPath(MAXFULLPATH, sztemp);
string str1, strFile;
str1 = sztemp;
strFile = str1 + "\\test.txt";
bOK = ff.Open(strFile, file::modeCreate | file::modeReadWrite );
int bb[4] = {1, 2, 3, 4};
ff.Write(bb, sizeof(bb));
int aa;
ff.Read(&aa, sizeof(aa)); //aa equals 1 now.
ff.Close();
Example 3:
//This console program is used to copy files.
int test()
{
char sztemp[MAXFULLPATH];
DWORD aa = GetTempPath(MAXFULLPATH, sztemp);
string str1, strFile, strFile2;
str1 = sztemp;
strFile = str1 + "\\test.txt";
strFile2 = str1 + "\\dest.txt";
// constructing these file objects doesn't open them
file sourceFile;
file destFile;
// open the source file for reading
bool bOK = sourceFile.Open(strFile, file::modeRead | file::shareDenyWrite);
if(!bOK)
{
out_str("failed to open the source file");
return 0;
}
// open the destination file for writing
bOK = destFile.Open(strFile2, file::modeWrite | file::shareExclusive | file::modeCreate);
if(!bOK)
{
out_str("failed to open the destination file");
return 0;
}
BYTE buffer[196];
DWORD dwRead;
// Read in 196-byte blocks. Remember how many bytes were actually read, and try to
// write that many out. This loop ends when there are no more bytes to read.
do
{
dwRead = sourceFile.Read(buffer, 196);
destFile.Write(buffer, dwRead);
}
while (dwRead > 0);
// Close both files
destFile.Close();
sourceFile.Close();
return 1;
}
Parameters:
lpszFileName=A string containing an absolute or relative path to the desired file.
nOpenFlags=Specifies the share and access modes of the opened file. See the default File constructor for
definitions of all supported modes. Modes can be combined using the bitwise OR operator "|". At least one access
and one share mode should be specified.
Return:
Returns FALSE or 0 if the file fails to open and returns TRUE or a non-zero if the file opens successfully.
SeeAlso:
file::file,file::Close.
*/
BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags); // Open a file returning FALSE or 0 on failure and TRUE or a non-zero on success.
/**
Remarks:
Read data into a buffer from the file associated with the file object.
Example:
//read integer and array of integers.
file ff;
bool bOK = ff.Open("c:\\test.dat", file::modeRead);
if(!bOK)
{
out_str("Failed to open file C:\test.txt");
return 0;
}
int aa, intlist[10];
ff.Read(&aa, sizeof(aa));
ff.Read(intlist, sizeof(intlist));
ff.Read(&intlist[0], sizeof(intlist));
//read double
double bb;
ff.Read(&bb, sizeof(bb));
//read structure
WIN32_FIND_DATAA cc;
ff.Read(&cc, sizeof(cc));
//read char array
char dd[100];
ff.Read(dd, sizeof(dd));
//read vector
vector<int> kk;
kk.SetSize(5);
int size = sizeof(int) * kk.GetSize();
ff.Read(kk, size);
Parameters:
lpBuf=A buffer where the data read is stored.
nCount=The number of bytes to read from the file indicated by the file object.
Return:
Number of bytes read from the file. May be less than nCount if EOF is reached.
SeeAlso:
File::Write.
*/
UINT Read(void *lpBuf, UINT nCount);
/**
Remarks:
Write data from a buffer into the file associated with the file object.
Example:
int test_write()
{
file ff;
bool bOK = ff.Open("C:\\test.txt", file::modeCreate | file::modeReadWrite);
if(!bOK)
{
out_str("Failed to open file C:\test.txt");
return 0;
}
int ia = 10;
ff.Write(&ia, sizeof(ia)); //write 4 to file.
int ib[4] = {1, 2, 3, 4};
ff.Write(ib, sizeof(ib)); //write 1, 2, 3, 4 to file following previous 4.
//write double
double da = 123.345;
ff.Write(&da, sizeof(da));
//write structure
WIN32_FIND_DATAA sa;
memset(&sa, 0, sizeof(WIN32_FIND_DATAA));
sa.dwFileAttributes = 20000;
sa.ftCreationTime.dwLowDateTime = 10;
sa.ftCreationTime.dwHighDateTime = 50;
sa.ftLastAccessTime.dwLowDateTime = 100000;
sa.ftLastAccessTime.dwHighDateTime = 500000;
sa.ftLastWriteTime.dwLowDateTime = 1000;
sa.ftLastWriteTime.dwHighDateTime = 5000;
sa.nFileSizeHigh = 9000000;
sa.nFileSizeLow = 1;
sa.dwReserved0 = 600;
sa.dwReserved1 = 98745;
lstrcpy(sa.cFileName, "C:\\Origin70\\OriginC\\origin.h");
lstrcpy(sa.cAlternateFileName,"originc.h");
ff.Write(&sa, sizeof(sa));
//write char array
char ca[20] = "This is a string.";
ff.Write(ca, sizeof(ca));
//write vector
vector<int> ik = {1, 2, 3, 4, 5};
int size = sizeof(int) * ik.GetSize();
ff.Write(ik, size);
return 1;
}
Parameters:
lpBuf=A buffer where the data to be written is stored.
nCount=The number of bytes to write from the buffer to the file indicated by the File object. Carriage return-line
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -