Contents Up Previous Next

wxTextFile

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 thus 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 small files like configuration files or program 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

wxFile

Members

wxTextFile::wxTextFile
wxTextFile::wxTextFile
wxTextFile::~wxTextFile
wxTextFile::AddLine
wxTextFile::Close
wxTextFile::Create
wxTextFile::Exists
wxTextFile::IsOpened
wxTextFile::GetLineCount
wxTextFile::GetLine
wxTextFile::operator[]
wxTextFile::GetCurrentLine
wxTextFile::GoToLine
wxTextFile::Eof
wxTextFile::GetEOL
wxTextFile::GetFirstLine
wxTextFile::GetNextLine
wxTextFile::GetPrevLine
wxTextFile::GetLastLine
wxTextFile::GetLineType
wxTextFile::GuessType
wxTextFile::GetName
wxTextFile::InsertLine
wxTextFile::Open
wxTextFile::RemoveLine
wxTextFile::Clear
wxTextFile::Write


wxTextFile::wxTextFile

wxTextFile() const

Default constructor, use Create or Open with a file name parameter to initialize the object.


wxTextFile::wxTextFile

wxTextFile(const wxString& strFile) const

Constructor does not load the file into memory, use Open() to do it.


wxTextFile::~wxTextFile

~wxTextFile() const

Destructor does nothing.


wxTextFile::AddLine

void AddLine(const wxString& str, wxTextFileType type = typeDefault) const

Adds a line to the end of file.


wxTextFile::Close

bool Close() const

Closes the file and frees memory, losing all changes. Use Write() if you want to save them.


wxTextFile::Create

bool Create() const

bool Create(const wxString& strFile) const

Creates the file with the given name or the name which was given in the constructor. The array of file lines is initially empty.

It will fail if the file already exists, Open should be used in this case.


wxTextFile::Exists

bool Exists() const

Return true if file exists - the name of the file should have been specified in the constructor before calling Exists().


wxTextFile::IsOpened

bool IsOpened() const

Returns true if the file is currently opened.


wxTextFile::GetLineCount

size_t GetLineCount() const

Get the number of lines in the file.


wxTextFile::GetLine

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.


wxTextFile::operator[]

wxString& operator[](size_t n) const

The same as GetLine.


wxTextFile::GetCurrentLine

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().


wxTextFile::GoToLine

void GoToLine(size_t n) const

Changes the value returned by GetCurrentLine and used by GetFirstLine()/GetNextLine().


wxTextFile::Eof

bool Eof() const

Returns true if the current line is the last one.


wxTextFile::GetEOL

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 (including Mac OS X when compiling with the Apple Developer Tools) and wxTextFileType_Mac under Mac OS (including Mac OS X when compiling with CodeWarrior).


wxTextFile::GetFirstLine

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:

wxTextFile file;
...
for ( str = file.GetFirstLine(); !file.Eof(); str = file.GetNextLine() )
{
    // do something with the current line in str
}
// do something with the last line in str

wxTextFile::GetNextLine

wxString& GetNextLine()

Gets the next line (see GetFirstLine for the example).


wxTextFile::GetPrevLine

wxString& GetPrevLine()

Gets the previous line in the file.


wxTextFile::GetLastLine

wxString& GetLastLine()

Gets the last line of the file. Together with GetPrevLine it allows to enumerate the lines in the file from the end to the beginning like this:

wxTextFile file;
...
for ( str = file.GetLastLine();
      file.GetCurrentLine() > 0;
      str = file.GetPrevLine() )
{
    // do something with the current line in str
}
// do something with the first line in str

wxTextFile::GetLineType

wxTextFileType GetLineType(size_t n) const

Get the type of the line (see also GetEOL)


wxTextFile::GuessType

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.


wxTextFile::GetName

const char* GetName() const

Get the name of the file.


wxTextFile::InsertLine

void InsertLine(const wxString& str, size_t n, wxTextFileType type = typeDefault) const

Insert a line before the line number n.


wxTextFile::Open

bool Open(wxMBConv& conv = wxConvUTF8) const

bool Open(const wxString& strFile, wxMBConv& conv = wxConvUTF8) const

Open() opens the file with the given name or the name which was given in the constructor and also loads file in memory on success. It will fail if the file does not exist, Create should be used in this case.

The conv argument is only meaningful in Unicode build of wxWidgets when it is used to convert the file to wide character representation.


wxTextFile::RemoveLine

void RemoveLine(size_t n) const

Delete line number n from the file.


wxTextFile::Clear

void Clear() const

Delete all lines from the file, set current line number to 0.


wxTextFile::Write

bool Write(wxTextFileType typeNew = wxTextFileType_None, wxMBConv& conv = wxConvUTF8) 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.

The conv argument is only meaningful in Unicode build of wxWidgets when it is used to convert all lines to multibyte representation before writing them them to physical file.

Returns true if operation succeeded, false if it failed.