The wxTextFile is a simple class which allows to work with text files on line by line basis. It also understands the differences in line termination characters under different platforms and will not do anything bad to files with "non native" line termination sequences - in fact, it can be also used to modify the text files and change the line termination characters from one type (say DOS) to another (say Unix).
One word of warning: the class is not at all optimized for big files and so it will load the file entirely into memory when opened. Of course, you should not work in this way with large files (as an estimation, anything over 1 Megabyte is surely too big for this class). On the other hand, it is not a serious limitation for the small files like configuration files or programs sources which are well handled by wxTextFile.
The typical things you may do with wxTextFile in order are:
Derived from
No base class
Include files
<wx/textfile.h>
Data structures
The following constants identify the line termination type:
enum wxTextFileType { wxTextFileType_None, // incomplete (the last line of the file only) wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n' wxTextFileType_Dos, // 'CR' 'LF' wxTextFileType_Mac // 'CR' = 0xD = 13 = '\r' };See also
Members
wxTextFile::wxTextFile
wxTextFile::wxTextFile
wxTextFile::Exists
wxTextFile::Open
wxTextFile::Open
wxTextFile::Close
wxTextFile::IsOpened
wxTextFile::GetLineCount
wxTextFile::GetLine
wxTextFile::operator[]
wxTextFile::GetCurrentLine
wxTextFile::GoToLine
wxTextFile::Eof
wxTextFile::GetFirstLine
wxTextFile::GetNextLine
wxTextFile::GetPrevLine
wxTextFile::GetLastLine
wxTextFile::GetLineType
wxTextFile::GuessType
wxTextFile::GetName
wxTextFile::AddLine
wxTextFile::InsertLine
wxTextFile::RemoveLine
wxTextFile::Write
wxTextFile::GetEOL
wxTextFile::~wxTextFile
wxTextFile() const
Default constructor, use Open(string) to initialize the object.
wxTextFile(const wxString& strFile) const
Constructor does not load the file into memory, use Open() to do it.
bool Exists() const
Return TRUE if file exists - the name of the file should have been specified in the constructor before calling Exists().
bool Open() const
Open() opens the file with the name which was given in the constructor and also loads file in memory on success.
bool Open(const wxString& strFile) const
Same as Open() but allows to specify the file name (must be used if the default constructor was used to create the object).
bool Close() const
Closes the file and frees memory, losing all changes. Use Write() if you want to save them.
bool IsOpened() const
Returns TRUE if the file is currently opened.
size_t GetLineCount() const
Get the number of lines in the file.
wxString& GetLine(size_t n) const
Retrieves the line number n from the file. The returned line may be modified but you shouldn't add line terminator at the end - this will be done by wxTextFile.
wxString& operator[](size_t n) const
The same as GetLine.
size_t GetCurrentLine() const
Returns the current line: it has meaning only when you're using GetFirstLine()/GetNextLine() functions, it doesn't get updated when you're using "direct access" functions like GetLine(). GetFirstLine() and GetLastLine() also change the value of the current line, as well as GoToLine().
void GoToLine(size_t n) const
Changes the value returned by GetCurrentLine and used by GetFirstLine()/GetNextLine().
bool Eof() const
Returns TRUE if the current line is the last one.
wxString& GetFirstLine() const
This method together with GetNextLine() allows more "iterator-like" traversal of the list of lines, i.e. you may write something like:
for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { // do something with the current line in str }
wxString& GetNextLine()
Gets the next line (see GetFirstLine for the example).
wxString& GetPrevLine()
Gets the previous line in the file.
wxString& GetLastLine()
Gets the last line of the file.
wxTextFileType GetLineType(size_t n) const
Get the type of the line (see also GetEOL)
wxTextFileType GuessType() const
Guess the type of file (which is supposed to be opened). If sufficiently many lines of the file are in DOS/Unix/Mac format, the corresponding value will be returned. If the detection mechanism fails wxTextFileType_None is returned.
const char* GetName() const
Get the name of the file.
void AddLine(const wxString& str, wxTextFileType type = typeDefault) const
Adds a line to the end of file.
void InsertLine(const wxString& str, size_t n, wxTextFileType type = typeDefault) const
Insert a line before the line number n.
void RemoveLine(size_t n) const
Delete line number n from the file.
bool Write(wxTextFileType typeNew = wxTextFileType_None) const
Change the file on disk. The typeNew parameter allows you to change the file format (default argument means "don't change type") and may be used to convert, for example, DOS files to Unix.
Returns TRUE if operation succeeded, FALSE if it failed.
static const char* GetEOL(wxTextFileType type = typeDefault) const
Get the line termination string corresponding to given constant. typeDefault is the value defined during the compilation and corresponds to the native format of the platform, i.e. it will be wxTextFileType_Dos under Windows, wxTextFileType_Unix under Unix and wxTextFileType_Mac under Mac.
~wxTextFile() const
Destructor does nothing.