我们在编写程序的时候,最密不可分的就是对文件进行相应的操作,我们可以从文件中读取数据,可以将数据保存到文件,可以……
文件在进行读写操作之前要先打开,使用完毕要关闭。所谓打开文件,实际上是建立文件的各种有关信息,并使文件指针指向该文件,以便进行其它操作。关闭文件则断开指针与文件之间的联系,也就禁止再对该文件进行操作。
总而言之,言而总之,一言以蔽之,对文件的操作是非常重要的,下面我们就来介绍一下C++中是如何对文件进行操作的。
在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,对文件的操作是通过stream的子类fstream(file stream)来实现的,所以,要用这种方式操作文件,就必须加入头文件fstream.h。
打开文件
文件名
注意路径名中的斜杠要双写,如:
"D:\\MyFiles\\ReadMe.txt"
文件打开方式选项:
ios::in = 0x01, //供读,文件不存在则创建(ifstream默认的打开方式)
ios::out = 0x02, //供写,文件不存在则创建,若文件已存在则清空原内容(ofstream默认的打开方式)
ios::ate = 0x04, //文件打开时,指针在文件最后。可改变指针的位置,常和in、out联合使用
ios::app = 0x08, //供写,文件不存在则创建,若文件已存在则在原文件内容后写入新的内容,指针位置总在最后
ios::trunc = 0x10, //在读写前先将文件长度截断为0(默认)
ios::nocreate = 0x20, //文件不存在时产生错误,常和in或app联合使用
ios::noreplace = 0x40, //文件存在时产生错误,常和out联合使用
ios::binary = 0x80 //二进制格式文件
文件保护方式选择项:
filebuf::openprot; //默认的兼容共享方式
filebuf::sh_none; //独占,不共享
filebuf::sh_read; //读共享
filebuf::sh_write; //写共享
打开文件的方法
调用构造函数时指定文件名和打开模式
ifstream f("d:\\12.txt",ios::nocreate); //默认以 ios::in 的方式打开文件,文件不存在时操作失败
ofstream f("d:\\12.txt"); //默认以 ios::out的方式打开文件
fstream f("d:\\12.dat",ios::in|ios::out|ios::binary); //以读写方式打开二进制文件
使用Open成员函数
fstream f;
f.open("d:\\12.txt",ios::out); //利用同一对象对多个文件进行操作时要用到open函数
检查是否成功打开
成功:
if(f){...} //对ifstream、ofstream对象可用,fstream对象不可用。
if(f.good()){...}
失败:
if(!f){...} // !运算符已经重载
if(f.fail()){...}
读写操作
使用<<,>>运算符
只能进行文本文件的读写操作,用于二进制文件可能会产生错误。
使用函数成员 get、put、read、write等
经常和read配合使用的函数是gcount(),用来获得实际读取的字节数。
注意事项
打开方式中必须指定ios::binary,否则读写会出错
用read\write进行读写操作,而不能使用插入、提取运算符进行操作,否则会出错。
使用eof()函数检测文件是否读结束,使用gcount()获得实际读取的字节数
关闭文件
使用成员函数close,如:
f.close();
利用析构函数
对象生命期结束时会检查文件是否关闭,对没有关闭的文件进行关闭操作。
随机读写
通过移动文件读写指针,可在文件指定位置进行读写。
seekg(绝对位置); //绝对移动, //输入流操作
seekg(相对位置,参照位置); //相对操作
tellg(); //返回当前指针位置
seekp(绝对位置); //绝对移动, //输出流操作
seekp(相对位置,参照位置); //相对操作
tellp(); //返回当前指针位置
参照位置:
ios::beg = 0 //相对于文件头
ios::cur = 1 //相对于当前位置
ios::end = 2 //相对于文件尾
基于以上,我们就可以对文件流进行封装了。新建一个AL_File类,对所有操作进行封装。
稍微注意下字节问题(指ASCII字符(单字节)和Unicode字符(宽字节)。前者占一个字节,后者占两个字节)
- #define (UNICODE) || (_UNICODE)
- typedef char NCHAR;
- //file
- #define NCHARfstream std::wfstream
- #define NCHARifstream std::wifstream
- #define NCHARfostream std::wofstream
- #else //UNICODE || _UNICODE
- typedef WCHAR NCHAR;
- //file
- #define NCHARfstream std::fstream
- #define NCHARifstream std::ifstream
- #define NCHARfostream std::ofstream
- #endif // ASCI || UTF-8
- /**
- @file AL_File.h
- @brief AL_File File operations
- @author arvin
- @version 1.0 2013/03/14
- */
- #ifndef CXX_AL_FILE_H
- #define CXX_AL_FILE_H
- #include <fstream>
- typedef long AL_ERROR;
- class AL_File
- {
- public:
- enum FILE_REFERENCE_POS;
- /**
- * Construction
- *
- * @param
- * @return
- */
- AL_File();
- /**
- * Construction
- *
- * @param <IN> const NCHAR* cpFilePath
- * @param <IN> WORD wOpenModel
- static const WORD MODEL_IN = std::ios::in; // 0x01, for reading the file does not exist, create (ifstream Open default)
- static const WORD MODEL_OUT = std::ios::out; // 0x02, for writing, the file does not exist, to create, if the file already exists, clear the original content (ofstream default Open)
- static const WORD MODEL_ATE = std::ios::ate; // 0x04, when the file is opened, the pointer in the file last. Can change the position of the pointer, often used in combination and in, out
- static const WORD MODEL_APP = std::ios::app; // 0x08, for writing, the file does not exist, to create, to write new content after the original contents of the file if the file already exists, the total in the final pointer position
- static const WORD MODEL_TRUNC = std::ios::trunc; // 0x10, length of the file read and write before first truncated to 0 (default)
- static const WORD MODEL_NOCREATE = std::ios::_Nocreate; // 0x20, file does not exist error, often used in combination and in or app
- static const WORD MODEL_NOREPLACE = std::ios::_Noreplace; // 0x40, file exists generates an error when used in combination, often and out
- static const WORD MODEL_BINARY = std::ios::binary; // 0x80, binary format file
- * @param <IN> WORD wAccess
- static const WORD ACCESS_ORDINARY = 0; // 0: ordinary files, open access
- static const WORD ACCESS_READONLY = 1; // 1: read-only file
- static const WORD ACCESS_HIDDEN = 2; // 2: hidden file
- static const WORD ACCESS_SYSTEM = 4; // 4: System Files
- * @return
- * @note
- * @attention The default way to open a file to open the binary file reading and writing
- The default file ordinary files, open access
- */
- AL_File(const NCHAR* cpFilePath, WORD wOpenModel = MODEL_IN|MODEL_OUT|MODEL_BINARY, WORD wOpenAccess = ACCESS_ORDINARY);
- /**
- * Destruction
- *
- * @param
- * @return
- */
- ~AL_File(VOID);
- ///////////////////////////////////Open the file///////////////////////////////////////
- /**
- * Open (Open the file)
- *
- * @param <IN> const NCHAR* cpFilePath
- * @param <IN> WORD wOpenModel
- static const WORD MODEL_IN = std::ios::in; // 0x01, for reading the file does not exist, create (ifstream Open default)
- static const WORD MODEL_OUT = std::ios::out; // 0x02, for writing, the file does not exist, to create, if the file already exists, clear the original content (ofstream default Open)
- static const WORD MODEL_ATE = std::ios::ate; // 0x04, when the file is opened, the pointer in the file last. Can change the position of the pointer, often used in combination and in, out
- static const WORD MODEL_APP = std::ios::app; // 0x08, for writing, the file does not exist, to create, to write new content after the original contents of the file if the file already exists, the total in the final pointer position
- static const WORD MODEL_TRUNC = std::ios::trunc; // 0x10, length of the file read and write before first truncated to 0 (default)
- static const WORD MODEL_NOCREATE = std::ios::_Nocreate; // 0x20, file does not exist error, often used in combination and in or app
- static const WORD MODEL_NOREPLACE = std::ios::_Noreplace; // 0x40, file exists generates an error when used in combination, often and out
- static const WORD MODEL_BINARY = std::ios::binary; // 0x80, binary format file
- * @param <IN> WORD wAccess
- static const WORD ACCESS_ORDINARY = 0; // 0: ordinary files, open access
- static const WORD ACCESS_READONLY = 1; // 1: read-only file
- static const WORD ACCESS_HIDDEN = 2; // 2: hidden file
- static const WORD ACCESS_SYSTEM = 4; // 4: System Files
- * @return VOID
- * @note Note the slash in the path name to dual-write, such as: "\\MyFiles\\ReadMe.txt"
- * @attention The default way to open a file to open the binary file reading and writing
- The default file ordinary files, open access
- */
- VOID Open(const NCHAR* cpFilePath, WORD wOpenModel = MODEL_IN|MODEL_OUT|MODEL_BINARY, WORD wOpenAccess = ACCESS_ORDINARY);
- ////////////////////////////////Check if successfully opened//////////////////////////////////////////
- /**
- * Good (Check if successfully opened)
- *
- * @param VOID
- * @return BOOL
- * @note
- * @attention
- */
- BOOL Good(VOID);
- /**
- * Fail (Check if successfully opened)
- *
- * @param VOID
- * @return BOOL
- * @note
- * @attention
- */
- BOOL Fail(VOID);
- /**
- * IsOpen (Check if successfully opened)
- *
- * @param VOID
- * @return BOOL
- * @note Generally use this method to determine if a file is open successfully
- * @attention
- */
- BOOL IsOpen(VOID);
- /////////////////////////////////Reading and writing binary files/////////////////////////////////////////
- /**
- * Put (Reading and writing binary files)
- *
- * @param <IN> NCHAR ch
- * @return VOID
- * @note put () function to write to the stream a character prototype ofstream & put (char ch),
- the use of relatively simple, such as file1.put ('c'); is to write to the stream a character 'c'.
- * @attention
- */
- VOID Put(NCHAR ch);
- /**
- * Get (Reading and writing binary files)
- *
- * @param <OUT> NCHAR& ch
- * @return VOID
- * @note put () corresponds to the form: ifstream & get (char & ch); function is to read a single character
- from the stream, and the result is stored in the reference ch, if the end of the file, and returns
- a null character. As file2.get (x); represents a character read from the file, and read characters
- stored in x.
- * @attention
- */
- VOID Get(NCHAR& ch);
- /**
- * Get (Reading and writing binary files)
- *
- * @param <OUT> NCHAR* pStr
- * @param <IN> DWORD dwGetNum
- * @param <IN> NCHAR chEndChar
- * @return VOID
- * @note ifstream & get (char * buf, int num, char delim = '\ n'); this form to read characters into the
- array pointed to by buf until reads num characters or encounter characters specified by delim,
- ifdelim this parameter will use the default value of the newline character '\ n'. For example:
- file2.get (str1, 127, 'A') ;/ / read characters from a file into a string str1 terminate when
- to encounter characters 'A' or read 127 characters.
- * @attention
- */
- VOID Get(NCHAR* pStr, DWORD dwGetNum, NCHAR chEndChar);
- /**
- * Read (Reading and writing binary files)
- *
- * @param <OUT> NCHAR* buf
- * @param <IN> DWORD dwNum
- * @return BOOL
- * @note Prototype: read (unsigned char * buf, int num); read () num characters to read from the file
- cache buf points to the end of the file has not been read into the num characters can use member
- functionsint gcount (); to get the actual number of characters read
- * @attention
- */
- VOID Read(NCHAR* buf, DWORD dwNum);
- /**
- * Write (Reading and writing binary files)
- *
- * @param <IN> NCHAR* buf
- * @param <IN> DWORD dwNum
- * @return BOOL
- * @note Prototype: write (const unsigned char * buf, int num); write () from buf points to the cache
- write num characters to the file, it is noteworthy cache type is unsigned char *, may sometimes
- be necessary type conversion.
- * @attention
- */
- VOID Write(const NCHAR* buf, DWORD dwNum);
- /**
- * Eof End of file is read (Reading and writing binary files)
- *
- * @param VOID
- * @return BOOL
- * @note
- * @attention
- */
- BOOL Eof(VOID);
- /**
- * Gcount The actual number of bytes read (Reading and writing binary files)
- *
- * @param VOID
- * @return DWORD
- * @note
- * @attention
- */
- DWORD Gcount(VOID);
- /**
- * Seekg Absolutely move (Reading and writing binary files)
- *
- * @param <IN> DWORD dwSeek absolute move position
- * @return VOID
- * @note
- * @attention Input stream operation
- */
- VOID Seekg(DWORD dwSeek);
- /**
- * Seekg Relatively move (Reading and writing binary files)
- *
- * @param <IN> DWORD dwSeek relatively move position
- * @param <IN> FILE_REFERENCE_POS eFileRefPos file reference position
- FILE_REFERENCE_POS_BEG = std::ios :: beg, // 0: relative to the file header
- FILE_REFERENCE_POS_CUR = std::ios :: cur, // 1: relative to the current position
- FILE_REFERENCE_POS_END = std::ios :: end, // 2: relative to the end of the file
- * @return VOID
- * @note
- * @attention Input stream operation
- */
- VOID Seekg(DWORD dwSeek, FILE_REFERENCE_POS eFileRefPos);
- /**
- * Tellg Returns the current pointer position (Reading and writing binary files)
- *
- * @param VOID
- * @return VOID
- * @note
- * @attention Input stream operation
- */
- VOID Tellg(VOID);
- /**
- * Seekp Absolutely move (Reading and writing binary files)
- *
- * @param <IN> DWORD dwSeek absolute move position
- * @return VOID
- * @note
- * @attention Output stream operation
- */
- VOID Seekp(DWORD dwSeek);
- /**
- * Seekp Relatively move (Reading and writing binary files)
- *
- * @param <IN> DWORD dwSeek relatively move position
- * @param <IN> FILE_REFERENCE_POS eFileRefPos file reference position
- FILE_REFERENCE_POS_BEG = std::ios :: beg, // 0: relative to the file header
- FILE_REFERENCE_POS_CUR = std::ios :: cur, // 1: relative to the current position
- FILE_REFERENCE_POS_END = std::ios :: end, // 2: relative to the end of the file
- * @return VOID
- * @note
- * @attention Output stream operation
- */
- VOID Seekp(DWORD dwSeek, FILE_REFERENCE_POS eFileRefPos);
- /**
- * Tellp Returns the current pointer position (Reading and writing binary files)
- *
- * @param VOID
- * @return VOID
- * @note
- * @attention Output stream operation
- */
- VOID Tellp(VOID);
- /////////////////////////////////////Close the file/////////////////////////////////////
- /**
- * Close (Close the file)
- *
- * @param VOID
- * @return VOID
- * @note
- * @attention
- */
- VOID Close(VOID);
- protected:
- private:
- /**
- *Copy Construct
- *
- * @param const AL_File& cAL_File
- * @return
- */
- AL_File(const AL_File& cAL_File);
- /**
- *Assignment
- *
- * @param const AL_File& cAL_File
- * @return AL_File&
- */
- AL_File &operator =(const AL_File& cAL_File);
- public:
- ////////////////////////////open model//////////////////////////////////////////////
- static const WORD MODEL_IN = std::ios::in; // 0x01, for reading the file does not exist, create (ifstream Open default)
- static const WORD MODEL_OUT = std::ios::out; // 0x02, for writing, the file does not exist, to create, if the file already exists, clear the original content (ofstream default Open)
- static const WORD MODEL_ATE = std::ios::ate; // 0x04, when the file is opened, the pointer in the file last. Can change the position of the pointer, often used in combination and in, out
- static const WORD MODEL_APP = std::ios::app; // 0x08, for writing, the file does not exist, to create, to write new content after the original contents of the file if the file already exists, the total in the final pointer position
- static const WORD MODEL_TRUNC = std::ios::trunc; // 0x10, length of the file read and write before first truncated to 0 (default)
- static const WORD MODEL_NOCREATE = std::ios::_Nocreate; // 0x20, file does not exist error, often used in combination and in or app
- static const WORD MODEL_NOREPLACE = std::ios::_Noreplace; // 0x40, file exists generates an error when used in combination, often and out
- static const WORD MODEL_BINARY = std::ios::binary; // 0x80, binary format file
- ////////////////////////////open access//////////////////////////////////////////////
- static const WORD ACCESS_ORDINARY = 0; // 0: ordinary files, open access
- static const WORD ACCESS_READONLY = 1; // 1: read-only file
- static const WORD ACCESS_HIDDEN = 2; // 2: hidden file
- static const WORD ACCESS_SYSTEM = 4; // 4: System Files
- //////////////////////////////////////////////////////////////////////////
- enum FILE_REFERENCE_POS
- {
- FILE_REFERENCE_POS_BEG = std::ios :: beg, // 0: relative to the file header
- FILE_REFERENCE_POS_CUR = std::ios :: cur, // 1: relative to the current position
- FILE_REFERENCE_POS_END = std::ios :: end, // 2: relative to the end of the file
- };
- protected:
- private:
- NCHARfstream* m_pfstream;
- };
- #endif // CXX_AL_FILE_H
- /* EOF */
- /**
- @file AL_File.h
- @brief AL_File File operations
- @author arvin
- @version 1.0 2013/03/14
- */
- #include "stdafx.h"
- #ifndef CXX_AL_FILE_H
- #include "AL_File.h"
- #endif
- #ifndef CXX_AL_ERROR_H
- #include "AL_Error.h"
- #endif
- /**
- * Construction
- *
- * @param
- * @return
- */
- AL_File::AL_File():
- m_pfstream(NULL)
- {
- m_pfstream = new NCHARfstream;
- }
- /**
- * Construction
- *
- * @param <IN> const NCHAR* cpFilePath
- * @param <IN> WORD wOpenModel
- static const WORD MODEL_IN = std::ios::in; // 0x01, for reading the file does not exist, create (ifstream Open default)
- static const WORD MODEL_OUT = std::ios::out; // 0x02, for writing, the file does not exist, to create, if the file already exists, clear the original content (ofstream default Open)
- static const WORD MODEL_ATE = std::ios::ate; // 0x04, when the file is opened, the pointer in the file last. Can change the position of the pointer, often used in combination and in, out
- static const WORD MODEL_APP = std::ios::app; // 0x08, for writing, the file does not exist, to create, to write new content after the original contents of the file if the file already exists, the total in the final pointer position
- static const WORD MODEL_TRUNC = std::ios::trunc; // 0x10, length of the file read and write before first truncated to 0 (default)
- static const WORD MODEL_NOCREATE = std::ios::_Nocreate; // 0x20, file does not exist error, often used in combination and in or app
- static const WORD MODEL_NOREPLACE = std::ios::_Noreplace; // 0x40, file exists generates an error when used in combination, often and out
- static const WORD MODEL_BINARY = std::ios::binary; // 0x80, binary format file
- * @param <IN> WORD wAccess
- static const WORD ACCESS_ORDINARY = 0; // 0: ordinary files, open access
- static const WORD ACCESS_READONLY = 1; // 1: read-only file
- static const WORD ACCESS_HIDDEN = 2; // 2: hidden file
- static const WORD ACCESS_SYSTEM = 4; // 4: System Files
- * @return
- * @note
- * @attention The default way to open a file to open the binary file reading and writing
- The default file ordinary files, open access
- */
- AL_File::AL_File(const NCHAR* cpFilePath, WORD wOpenModel, WORD wOpenAccess):
- m_pfstream(NULL)
- {
- m_pfstream = new NCHARfstream;
- Open(cpFilePath, wOpenModel, wOpenAccess);
- }
- /**
- * Destruction
- *
- * @param
- * @return
- */
- AL_File::~AL_File(VOID)
- {
- if (NULL == m_pfstream) {
- delete m_pfstream;
- m_pfstream = NULL;
- }
- }
- ///////////////////////////////////Open the file///////////////////////////////////////
- /**
- * Open (Open the file)
- *
- * @param <IN> const NCHAR* cpFilePath
- * @param <IN> WORD wOpenModel
- static const WORD MODEL_IN = std::ios::in; // 0x01, for reading the file does not exist, create (ifstream Open default)
- static const WORD MODEL_OUT = std::ios::out; // 0x02, for writing, the file does not exist, to create, if the file already exists, clear the original content (ofstream default Open)
- static const WORD MODEL_ATE = std::ios::ate; // 0x04, when the file is opened, the pointer in the file last. Can change the position of the pointer, often used in combination and in, out
- static const WORD MODEL_APP = std::ios::app; // 0x08, for writing, the file does not exist, to create, to write new content after the original contents of the file if the file already exists, the total in the final pointer position
- static const WORD MODEL_TRUNC = std::ios::trunc; // 0x10, length of the file read and write before first truncated to 0 (default)
- static const WORD MODEL_NOCREATE = std::ios::_Nocreate; // 0x20, file does not exist error, often used in combination and in or app
- static const WORD MODEL_NOREPLACE = std::ios::_Noreplace; // 0x40, file exists generates an error when used in combination, often and out
- static const WORD MODEL_BINARY = std::ios::binary; // 0x80, binary format file
- * @param <IN> WORD wAccess
- static const WORD ACCESS_ORDINARY = 0; // 0: ordinary files, open access
- static const WORD ACCESS_READONLY = 1; // 1: read-only file
- static const WORD ACCESS_HIDDEN = 2; // 2: hidden file
- static const WORD ACCESS_SYSTEM = 4; // 4: System Files
- * @return VOID
- * @note Note the slash in the path name to dual-write, such as: "\\MyFiles\\ReadMe.txt"
- * @attention The default way to open a file to open the binary file reading and writing
- */
- VOID
- AL_File::Open(const NCHAR* cpFilePath, WORD wOpenModel, WORD wOpenAccess)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->open(cpFilePath, wOpenModel, wOpenAccess);
- }
- ////////////////////////////////Check if successfully opened//////////////////////////////////////////
- /**
- * Good (Check if successfully opened)
- *
- * @param VOID
- * @return BOOL
- * @note
- * @attention
- */
- BOOL
- AL_File::Good(VOID)
- {
- if (NULL == m_pfstream) {
- return FALSE;
- }
- return m_pfstream->good();
- }
- /**
- * Fail (Check if successfully opened)
- *
- * @param VOID
- * @return BOOL
- * @note
- * @attention
- */
- BOOL
- AL_File::Fail(VOID)
- {
- if (NULL == m_pfstream) {
- return FALSE;
- }
- return m_pfstream->fail();
- }
- /**
- * IsOpen (Check if successfully opened)
- *
- * @param VOID
- * @return BOOL
- * @note Generally use this method to determine if a file is open successfully
- * @attention
- */
- BOOL
- AL_File::IsOpen(VOID)
- {
- if (NULL == m_pfstream) {
- return FALSE;
- }
- return m_pfstream->is_open();
- }
- /////////////////////////////////Reading and writing binary files/////////////////////////////////////////
- /**
- * Put (Reading and writing binary files)
- *
- * @param <IN> NCHAR ch
- * @return VOID
- * @note put () function to write to the stream a character prototype ofstream & put (char ch),
- the use of relatively simple, such as file1.put ('c'); is to write to the stream a character 'c'.
- * @attention
- */
- VOID
- AL_File::Put(NCHAR ch)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->put(ch);
- }
- /**
- * Get (Reading and writing binary files)
- *
- * @param <OUT> NCHAR& ch
- * @return VOID
- * @note put () corresponds to the form: ifstream & get (char & ch); function is to read a single character
- from the stream, and the result is stored in the reference ch, if the end of the file, and returns
- a null character. As file2.get (x); represents a character read from the file, and read characters
- stored in x.
- * @attention
- */
- VOID
- AL_File::Get(NCHAR& ch)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->get(ch);
- }
- /**
- * Get (Reading and writing binary files)
- *
- * @param <OUT> NCHAR* pStr
- * @param <IN> DWORD dwGetNum
- * @param <IN> NCHAR chEndChar
- * @return VOID
- * @note ifstream & get (char * buf, int num, char delim = '\ n'); this form to read characters into the
- array pointed to by buf until reads num characters or encounter characters specified by delim,
- ifdelim this parameter will use the default value of the newline character '\ n'. For example:
- file2.get (str1, 127, 'A') ;/ / read characters from a file into a string str1 terminate when
- to encounter characters 'A' or read 127 characters.
- * @attention
- */
- VOID
- AL_File::Get(NCHAR* pStr, DWORD dwGetNum, NCHAR chEndChar)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->get(pStr, dwGetNum, chEndChar);
- }
- /**
- * Read (Reading and writing binary files)
- *
- * @param <OUT> NCHAR* buf
- * @param <IN> DWORD dwNum
- * @return BOOL
- * @note Prototype: read (unsigned char * buf, int num); read () num characters to read from the file
- cache buf points to the end of the file has not been read into the num characters can use member
- functionsint gcount (); to get the actual number of characters read
- * @attention
- */
- VOID
- AL_File::Read(NCHAR* buf, DWORD dwNum)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->read(buf, dwNum);
- }
- /**
- * Write (Reading and writing binary files)
- *
- * @param <IN> NCHAR* buf
- * @param <IN> DWORD dwNum
- * @return BOOL
- * @note Prototype: write (const unsigned char * buf, int num); write () from buf points to the cache
- write num characters to the file, it is noteworthy cache type is unsigned char *, may sometimes
- be necessary type conversion.
- * @attention
- */
- VOID
- AL_File::Write(const NCHAR* buf, DWORD dwNum)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->write(buf, dwNum);
- }
- /**
- * Eof End of file is read (Reading and writing binary files)
- *
- * @param VOID
- * @return BOOL
- * @note
- * @attention
- */
- BOOL
- AL_File::Eof(VOID)
- {
- if (NULL == m_pfstream) {
- return FALSE;
- }
- return m_pfstream->eof();
- }
- /**
- * Gcount The actual number of bytes read (Reading and writing binary files)
- *
- * @param VOID
- * @return DWORD
- * @note
- * @attention
- */
- DWORD
- AL_File::Gcount(VOID)
- {
- if (NULL == m_pfstream) {
- return FALSE;
- }
- return m_pfstream->gcount();
- }
- /**
- * Seekg Absolutely move (Reading and writing binary files)
- *
- * @param <IN> DWORD dwSeek absolute move position
- * @return VOID
- * @note
- * @attention Input stream operation
- */
- VOID
- AL_File::Seekg(DWORD dwSeek)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->seekg(dwSeek);
- }
- /**
- * Seekg Relatively move (Reading and writing binary files)
- *
- * @param <IN> DWORD dwSeek relatively move position
- * @param <IN> FILE_REFERENCE_POS eFileRefPos file reference position
- FILE_REFERENCE_POS_BEG = std::ios :: beg, // 0: relative to the file header
- FILE_REFERENCE_POS_CUR = std::ios :: cur, // 1: relative to the current position
- FILE_REFERENCE_POS_END = std::ios :: end, // 2: relative to the end of the file
- * @return VOID
- * @note
- * @attention Input stream operation
- */
- VOID
- AL_File::Seekg(DWORD dwSeek, FILE_REFERENCE_POS eFileRefPos)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->seekg(dwSeek, eFileRefPos);
- }
- /**
- * Tellg Returns the current pointer position (Reading and writing binary files)
- *
- * @param VOID
- * @return VOID
- * @note
- * @attention Input stream operation
- */
- VOID
- AL_File::Tellg(VOID)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->tellg();
- }
- /**
- * Seekp Absolutely move (Reading and writing binary files)
- *
- * @param <IN> DWORD dwSeek absolute move position
- * @return VOID
- * @note
- * @attention Output stream operation
- */
- VOID
- AL_File::Seekp(DWORD dwSeek)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->seekp(dwSeek);
- }
- /**
- * Seekp Relatively move (Reading and writing binary files)
- *
- * @param <IN> DWORD dwSeek relatively move position
- * @param <IN> FILE_REFERENCE_POS eFileRefPos file reference position
- FILE_REFERENCE_POS_BEG = std::ios :: beg, // 0: relative to the file header
- FILE_REFERENCE_POS_CUR = std::ios :: cur, // 1: relative to the current position
- FILE_REFERENCE_POS_END = std::ios :: end, // 2: relative to the end of the file
- * @return VOID
- * @note
- * @attention Output stream operation
- */
- VOID
- AL_File::Seekp(DWORD dwSeek, FILE_REFERENCE_POS eFileRefPos)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->seekp(dwSeek, eFileRefPos);
- }
- /**
- * Tellp Returns the current pointer position (Reading and writing binary files)
- *
- * @param VOID
- * @return VOID
- * @note
- * @attention Output stream operation
- */
- VOID
- AL_File::Tellp(VOID)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->tellp();
- }
- /////////////////////////////////////Close the file/////////////////////////////////////
- /**
- * Close (Close the file)
- *
- * @param VOID
- * @return VOID
- * @note
- * @attention
- */
- VOID
- AL_File::Close(VOID)
- {
- if (NULL == m_pfstream) {
- return;
- }
- m_pfstream->close();
- }
- /* EOF */
附其它几篇相关文章:
http://www.iteye.com/topic/383903
http://blog.csdn.net/mak0000/article/details/3230199
文件路径函数
ExpandFileName() 返回文件的全路径(含驱动器、路径)
ExtractFileExt() 从文件名中抽取扩展名
ExtractFileName() 从文件名中抽取不含路径的文件名
ExtractFilePath() 从文件名中抽取路径名
ExtractFileDir() 从文件名中抽取目录名
ExtractFileDrive() 从文件名中抽取驱动器名
ChangeFileExt() 改变文件的扩展名
ExpandUNCFileName() 返回含有网络驱动器的文件全路径
ExtractRelativePath() 从文件名中抽取相对路径信息
ExtractShortPathName() 把文件名转化为DOS的8·3格式
MatchesMask() 检查文件是否与指定的文件名格式匹配
tellp(); //返回当前指针位置
参照位置:
ios::beg = 0 //相对于文件头
ios::cur = 1 //相对于当前位置
ios::end = 2 //相对于文件尾
文件管理函数
这类函数包括设置和读取驱动器、子目录和文件的有关的各种操作,下表列出这类操作常用的函数及其功能。
函数 功能
CreateDir() 创建新的子目录
DeleteFile() 删除文件
DirectoryExists() 判断目录是否存在
DiskFree() 获取磁盘剩余空间
DiskSize() 获取磁盘容量
FileExists() 判断文件是否存在
FileGetAttr() 获取文件属性
FileGetDate() 获取文件日期
GetCurrentDir() 获取当前目录
RemoveDir() 删除目录
SetCurrentDir() 设置当前目录
⑴CreateDir()
原型:extern PACKAGE bool __fastcall CreateDir(const System::AnsiString Dir);
功能:建立子目录,如果成功返回true,否则返回false
参数:Dir:要建立的子目录的名字
例:Create("ASM");//在当前目录下建立一个名为ASM的子目录
⑵DeleteFile()
原型:extern PACKAGE bool __fastcall DeleteFile(const System::AnsiString FileName);
功能:删除文件,如果成功返回true,否则返回false
参数:FileName:要删除的文件名
例:if(OpenDialog1->Execute())DeleteFile(OpenDialog1->FileName);
⑶DirectoryExists()
原型:extern PACKAGE bool __fastcall DirectoryExists(const System:: AnsiString Name);
功能:检测目录是否存在,如果存在返回true,否则返回false
参数:Name:要检测的目录名
例:if(!DirectoryExists("ASM"))CreateDir("ASM");//如果ASM这个目录不存在则创建之
⑷DiskFree()
原型:extern PACKAGE __int64 __fastcall DiskFree(Byte Drive);
功能:检测磁盘剩余空间,返回值以字节为单位,如果指定的磁盘无效,返回-1
参数:Drive:磁盘的代号,0表示当前盘, 1=A,2=B,3=C 以此类推
例:ShowMessage(DiskFree(0));//显示当前盘的剩余空间
⑸DiskSize()
原型:extern PACKAGE __int64 __fastcall DiskSize(Byte Drive);
功能:检测磁盘容量,返回值以字节为单位,如果指定的磁盘无效,返回-1
参数:Drive:磁盘的代号,0表示当前盘, 1=A,2=B,3=C 以此类推
例:ShowMessage(DiskFree(0));//显示当前盘的容量
⑹FileExists()
原型:extern PACKAGE bool __fastcall FileExists(const AnsiString FileName);
功能:检测文件是否存在,如果存在返回true,否则返回false
参数:FileName:要检测的文件名
例:if(FileExists("AAA.ASM"))DeleteFile("AAA.ASM");
⑺FileGetAttr()
原型:extern PACKAGE int __fastcall FileGetAttr(const AnsiString FileName);
功能:取得文件属性,如果出错返回-1
返回值如下表,如果返回$00000006表示是一个具有隐含和系统属性的文件(4+2)
常量 值 含义
faReadOnly $00000001 只读文件
faHidden $00000002 隐含文件
faSysFile $00000004 系统文件
faVolumeID $00000008 卷标
faDirectory $00000010 目录
faArchive $00000020 归档文件
例:if(FileGetAttr("LLL.TXT")&0x2)ShowMessage("这是一个有隐含属性的文件");
与此对应的有FileSetAttr() ,请自已查阅帮助系统
⑻FileGetDate()
原型:extern PACKAGE int __fastcall FileGetDate(int Handle);
功能:返回文件的建立时间到1970-1-1日0时的秒数
参数:Handle:用FileOpen()打开的文件句柄。
例:
int i=FileOpen("C://autoexec.bat",fmOpenRead);
ShowMessage(FileGetDate(i));
FileClose(i);
与此对应的有FileSetDate(),请自已查阅帮助系统
⑼GetCurrentDir()
原型:extern PACKAGE AnsiString __fastcall GetCurrentDir();
功能:取得当前的目录名
例:ShowMessage(GetCurrentDir());
⑽RemoveDir()
原型:extern PACKAGE bool __fastcall RemoveDir(const AnsiString Dir);
功能:删除目录,如果成功返回true,否则返回false
参数:Dir:要删除的目录名
例:if(DiectoryExists("ASM"))RemoveDir("ASM");
⑾SetCurrentDir()
原型:extern PACKAGE bool __fastcall SetCurrentDir(const AnsiString Dir);
功能:设置当前目录,如果成功返回true,否则返回false
参数:Dir:要切换到的目录名
例:SetCurrentDir("C://WINDOWS");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以后的笔记潇汀会尽量详细讲解一些相关知识的,希望大家继续关注我的博客。
本节笔记到这里就结束了。
潇汀一有时间就会把自己的学习心得,觉得比较好的知识点写出来和大家一起分享。
编程开发的路很长很长,非常希望能和大家一起交流,共同学习,共同进步。
如果文章中有什么疏漏的地方,也请大家指正。也希望大家可以多留言来和我探讨编程相关的问题。
最后,谢谢你们一直的支持~~~
from:http://blog.csdn.net/xiaoting451292510/article/details/8677867