📄 file.h
字号:
feed character combinations are counted as one byte.
Return:
The number of bytes written to the file from the buffer.
SeeAlso:
File::Read,StdioFile::WriteString.
*/
UINT Write(void *lpBuf, UINT nCount);
/**
Remarks:
Get the handle of current open file.
Example:
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;
}
UINT hFile = ff.GetHandle();
Return:
A variable of type UINT. It is file::hFileNull (an operating-system-independent empty file indicator)
if the handle has not been assigned.
*/
UINT GetHandle();
/**
Remarks:
Moves the pointer to the current file position as specified. Seek enables random access within a file.
Example:
file ff;
bool bOK = ff.Open("C:\\test.txt", file::modeCreate);
if(!bOK)
{
out_str("Failed to open file C:\test.txt");
return 0;
}
LONG lCurrent = ff.Seek(20, file::begin); // lCurrent = 20.
lCurrent = ff.Seek(16, file::current); // lCurrent = 20 + 16 = 36.
lCurrent = ff.Seek(-16, file::end); // lCurrent = length of file - 16.
Parameters:
lOffset=Number of bytes to move the file pointer relative to and in the direction indicated by nFrom.
nFrom=Specifies the referenece position and direction to move the pointer to the current file position.
file::begin Moves the current file position lOffset bytes forward from the beginning of the file.
file::current Moves the current file position lOffset bytes from the current position in the file.
file::end Moves the current file position lOffset bytes backward from the end of the file. The value of
lOffset must be negative otherwise the current file position will be outside the file.
Return:
If the current file position is valid, seek returns the number of bytes from the beginning of the file
otherwise an exception is thrown.
SeeAlso:
file::GetPosition,file::SeekToBegin,file::SeekToEnd.
*/
LONG Seek(LONG lOffset, UINT nFrom);
/**
Remarks:
Sets the file pointer value to the beginning of the file.
This is the same as file::Seek(0, file::begin).
Examples:
// Assume strFile is a string containing the name of an exsiting file.
file ff;
strFile="C:\\test.dat";
if( ff.Open(strFile, file::modeRead) )
{
LONG lFileSize = ff.SeekToEnd(); // Go to the end of the file.
ff.SeekToBegin(); // Go to the beginning of the file.
ff.Close();
}
SeeAlso:
file::GetPosition,file::Seek,file::SeekToEnd.
*/
void SeekToBegin();
/**
Remarks:
Sets the file pointer value to the end of the file.
This is the same as file::Seek(0, file::end).
Examples:
// Assume strFile is a string containing the name of an exsiting file.
file ff;
strFile="C:\\test.dat";
if( ff.Open(strFile, file::modeRead) )
{
LONG lFileSize = ff.SeekToEnd();
ff.Close();
}
Return:
The number of bytes of the whole file
SeeAlso:
file::GetPosition,file::Seek,file::SeekToBegin.
*/
LONG SeekToEnd();
/**
Remarks:
Force data remaining in the file buffer to be written to 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) )
{
ff.Write(&strFile, strFile.GetLength()); // Write the file name into the file.
ff.Flush(); // Make sure the file buffer has been written into the file.
ff.Close();
}
Return:
TRUE for success and FALSE for error.
SeeAlso:
file::Write.
*/
BOOL Flush();
/**
Remarks:
Closes the file associated with the indicated file object. The file is then no longer available for reading and
writing.
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) )
{
ff.Write(strFile, strFile.GetLength()); // Write the file name into the file.
ff.Close(); // Close the file when we are done.
}
Return:
A value of FALSE or 0 indicates fail to close file and a value of TRUE or a
non-zero indicates sucessfully closing file.
SeeAlso:
file::Open.
*/
BOOL Close(); // Close the file associated with the indicated file object.
/**
Remarks:
Obtains the current logical length of the file in bytes.
Example:
string strFile = "c:\\myfile.txt";
file ff;
if( ff.Open(strFile, file::modeRead|file::typeBinary) )
{
DWORD dwLen = ff.GetLength();
printf("Length of file %s is %d bytes\n", strFile, dwLen);
ff.Close();
}
Return:
The length of the file.
*/
DWORD GetLength();
/**
Remarks:
It is used to support Little Endian and Big Endian read and write binary data .
Example:
#define INT32_ITEM_COUNT 2
#define INT16_ITEM_COUNT 4
// ASCII: A B C D 1 2 3 4
// HEX : 41 42 43 44 31 32 33 34
//
int test_read_endian()
{
file fil;
short i16[INT16_ITEM_COUNT];
long i32[INT32_ITEM_COUNT];
i16[0] = 0x4241;
i16[1] = 0x4443;
i16[2] = 0x3231;
i16[3] = 0x3433;
i32[0] = 0x44434241;
i32[1] = 0x34333231;
string strFile = "C:\\end.bin";
if( fil.Open(strFile, file::modeCreate | file::modeWrite ) )
{
int i = fil.WriteInt(i16, sizeof(short), INT16_ITEM_COUNT);
ASSERT( INT16_ITEM_COUNT == i);
i = fil.WriteInt(i32, sizeof(long), INT32_ITEM_COUNT);
ASSERT( INT32_ITEM_COUNT == i);
i = fil.WriteInt(i16, sizeof(short), INT16_ITEM_COUNT, FALSE);
ASSERT( INT16_ITEM_COUNT == i);
i = fil.WriteInt(i32, sizeof(long), INT32_ITEM_COUNT, FALSE);
ASSERT( INT32_ITEM_COUNT == i);
fil.Close();
}
else
{
string strPromptExist = "Before testing, please delete file: " + strFile;
MessageBox( NULL, strPromptExist );
}
if( fil.Open(strFile, file::modeRead|file::shareDenyWrite) )
{
int i = fil.ReadInt(i16, sizeof(short), INT16_ITEM_COUNT);
ASSERT( INT16_ITEM_COUNT == i);
ASSERT( i16[0] == 0x4241);
ASSERT( i16[1] == 0x4443);
ASSERT( i16[2] == 0x3231);
ASSERT( i16[3] == 0x3433);
fil.SeekToBegin();
i = fil.ReadInt(i32, sizeof(long), INT32_ITEM_COUNT);
ASSERT( INT32_ITEM_COUNT == i);
ASSERT( i32[0] == 0x44434241);
ASSERT( i32[1] == 0x34333231);
fil.SeekToBegin();
i = fil.ReadInt(i16, sizeof(short), INT16_ITEM_COUNT, FALSE);
ASSERT( INT16_ITEM_COUNT == i);
ASSERT( i16[0] == 0x4142);
ASSERT( i16[1] == 0x4344);
ASSERT( i16[2] == 0x3132);
ASSERT( i16[3] == 0x3334);
fil.SeekToBegin();
i = fil.ReadInt(i32, sizeof(long), INT32_ITEM_COUNT, FALSE);
ASSERT( INT32_ITEM_COUNT == i);
ASSERT( i32[0] == 0x41424344);
ASSERT( i32[1] == 0x31323334);
fil.Close();
}
return 1;
}
Parameters:
nDataTypeSize: Must be size of datatype, for integal, 1, 2 and 4, for float. 4 and 8.
nItemCount: Number of data;
bIsLittleEndian: TRUE, use Little Endian way, FALSE use Big Endian way, default value is TRUE.
Return:
The number of items read from file.
*/
UINT ReadInt(void *pDestBuffer, int nDataTypeSize, UINT nItemCount, BOOL bIsLittleEndian = TRUE);
/**
SeeAlso:
file::ReadInt.
*/
UINT WriteInt(void *pSrcBuffer, int nDataTypeSize, UINT nItemCount, BOOL bIsLittleEndian = TRUE);
/**
Example:
int test_file_readfloat_writefloat()
{
file ff;
string strFile = "readwritefloat.dat";
BOOL bOK = ff.Open(strFile, file::modeCreate | file::modeWrite);
if(!bOK)
{
out_str("Could not open the \\readwritefloat.dat file.");
ASSERT(false);
return 0;
}
vector<float> vecfloat={2,4,6,8};
vector<double> vecdouble={1,3,5,7};
ff.WriteFloat(&vecfloat,sizeof(float),vecfloat.GetSize());
int nposition=ff.GetPosition();
ASSERT(nposition==16);
ff.WriteFloat(&vecdouble, sizeof(double), vecdouble.GetSize(), false);
nposition=ff.GetPosition();
ASSERT(nposition==48);
ff.Close();
bOK = ff.Open(strFile, file::modeRead);
if(!bOK)
{
out_str("Could not open the \\readwritefloat.dat file.");
ASSERT(false);
return 0;
}
ff.SeekToBegin();
vector<float> vecf2(4);
vector<double> vecd2(4);
ff.ReadFloat(&vecf2,sizeof(float),vecfloat.GetSize());
ff.ReadFloat(&vecd2,sizeof(double), vecdouble.GetSize(),false);
for(int i=0;i<4;i++)
{
printf("%f\t%f\n",vecf2[i],vecd2[i]);
ASSERT(round(vecfloat[i],8)==round(vecf2[i],8));
ASSERT(round(vecdouble[i],8)==round(vecd2[i],8));
}
ff.Close();
return 1;
}
SeeAlso:
file::ReadInt.
*/
UINT ReadFloat(void *pDestBuffer, int nDataTypeSize, UINT nItemCount, BOOL bIsLittleEndian = TRUE);
/**
SeeAlso:
file::ReadFloat.
*/
UINT WriteFloat(void *pSrcBuffer, int nDataTypeSize, UINT nItemCount, BOOL bIsLittleEndian = TRUE);
};
/** >System
The Origin C stdioFile class provides read/write access to text and binary files using
buffered stream io. However, the stdioFile class does not support stream io to stdin,
stdout, and stderr. See also the file class for unbuffered io to binary files. The stdioFile
class is derived from the file class from which it inherits methods and properties.
Example:
// Get the Origin folder path
string strOriginPath, strFile;
strOriginPath = GetAppPath() + "OriginC";
strFile = strOriginPath + "\\test.txt";
// Create a new standard I/O file.
stdioFile ff(strFile, file::modeCreate | file::modeReadWrite);
// Write strings
string strLine1 = "line 1:";
string strLine3 = "line 3:";
ff.WriteString(strLine1);
ff.WriteString("line 2:");
ff.WriteString(strLine3);
*/
class stdioFile : public file
{
public:
// Constructors
/**
Remarks:
The default constructor does not open a file but rather sets m_hFile to file::hFileNull.
Example:
stdioFile ff;
*/
stdioFile();
/**
Remarks:
Creates a stdioFile object and opens the file having the path specified by the parameter lpszFileName. This
constructor combines the functions of the default constructor stdioFile() 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:
//Create a new standard I/O file.
//Get the origin folder path.
string strOriginPath;
strOriginPath = GetAppPath() + "OriginC";
string strFile = strOriginPath + "\\test.txt";
stdioFile ff(strFile, file::modeCreate | file::modeReadWrite);
Parameters:
lpszFileName= Specifies a string that is the path to the desired file.
The path can be relative or absolute.
nOpenFlags= Sharing and access mode. Specifies the action to take when the file is opened.
*/
stdioFile(LPCTSTR lpszFileName, UINT nOpenFlags);
// Operations
/**
Remarks:
Write a line to a file. End of line character is appended.
Example:
//write string.
string strLine1 = "line 1:";
string strLine3 = "line 3:";
stdioFile ff("c:\\test.txt",file::modeCreate | file::modeReadWrite);
ff.WriteString(strLine1);
ff.WriteString("line 2:");
ff.WriteString(strLine3);
Parameter:
lpcsz = Specifies a pointer to a buffer containing a null-terminated string.
Return:
the length of lpcsz.
SeeAlso:
stdioFile::ReadString.
*/
int WriteString(LPCTSTR lpcsz);
/**
Read a line from a file into a string. The end of line character is removed.
Remarks:
strings in ascii file might be separated by "\r\n", as typical of files fom DOS/Windows
or can be just "\n" or just "\r", as typical of unix and MAC files. Both forms are supported by this function.
This function will also return TRUE on the last line even if it does not have a "\n" or "\r" termination. In
other words, when reaching the end of a file, the function returns FALSE and the resulting string will always be empty.
Please note a size limitation of 32762 characters for this function, so longer lines will be broken into multiple lines.
Example:
//the following example will type out any ascii files from the User folder
void show_file(string strfile = "origin.ini")
{
string strFilename = GetAppPath() + strfile;
stdioFile ff;
bool bRet = ff.Open(strFilename, file::modeRead);
if(!bRet)
{
out_str("file not found!");
return;
}
string strTemp;
while(ff.ReadString(strTemp))
out_str(strTemp);
ff.Close();
}
Parameter:
str=A reference to a string object that will contain the string when the function returns.
Return:
TRUE for success, FALSE for end of file reached, or other error.
SeeAlso:
stdioFile::WriteString.
*/
BOOL ReadString(string& str);
};
#endif //_FILE_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -