我們在編寫程序的時候,最密不可分的就是對文件進行相應的操作,我們可以從文件中讀取數據,可以將數據保存到文件,可以…… 文件在進行讀寫操作之前要先打開,使用完畢要關閉。所謂打開文件,實際上是建立文件的各種有關信息,並使文件指針指向該文件,以便進行其它操作。關閉文件則斷開指針與文件之間的聯系,也就禁止再對該文件進行操作。 總而言之,言而總之,一言以蔽之,對文件的操作是非常重要的,下面我們就來介紹一下C++中是如何對文件進行操作的。www.2cto.com 在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字符(寬字節)。前者占一個字節,後者占兩個字節) [cpp] view plaincopy #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 [cpp] view plaincopy /** @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 */ [cpp] view plaincopy /** @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:要切換到的目錄名