back to io 

wxTextFile

generated from ../src/mod_io/textfile.cpp

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:

  • Create and open it: this is done with either create or open function which opens the file (name may be specified either as the argument to these functions or in the constructor), reads its contents in memory (in the case of open) and closes it.
  • Work with the lines in the file: this may be done either with "direct access" properties like lines or with "sequential access" properties which include firstLine/nextLine and also lastLine/prevLine. For the sequential access properties the current line number is maintained: currentLine and may be changed with goToLine.
  • Add/remove lines to the file: addLine and insertLine add new lines while removeLine deletes the existing ones. clear resets the file to empty.
  • Save your changes: notice that the changes you make to the file will not be saved automatically; calling close or doing nothing discards them! To save the changes you must explicitly call write - here, you may also change the line termination type if you wish
Remark :wxJS adds the wxTextLine class. In wxWidgets, lines can be changed because most methods return a reference. This is not possible in JavaScript, but this can be solved by returning an object: wxTextLine. This class can be used to access the lines like they were an array and you can change them directly:

   var file = new wxTextFile("text.txt");
   file.line[0] = "This is the first line";
  

Constants

wxTextFileType

Name Description
None
Unix
Dos
Mac

Class Methods

getEOL

getEOL(Type = typeDefault) : String
 
Name Type Default Description
Type wxTextFileType typeDefault

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

Constructor

wxTextFile

wxTextFile()
 
wxTextFile(FileName)
 
Name Type Default Description
FileName String The filename

Constructs a new wxTextFile object. The file is not loaded into memory. Use open to do it.

Properties

Name Type Description
currentLine read only Integer Returns the current line: it has meaning only when you're using firstLine/nextLine properties, it doesn't get updated when you're using "direct access" property lines. firstLine and lastLine also change the value of the current line, as well as goToLine.
eof read only Boolean Returns true when end of file is reached. When the file is not open, undefined is returned.
firstLine wxTextLine Get the first line.
guessType read only wxTextFileType 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
lastLine wxTextLine Get the last line
lineCount read only Integer Number of lines in the file.
lines Array of wxTextLine Get an array of lines
nextLine wxTextLine Get the nextline
opened read only Boolean Is the file open?
prevLine wxTextLine Get the previous line

Methods

addLine

addLine(Line, 
        Type)
 
Name Type Default Description
Line String The line to add
Type wxTextFileType

Adds a line to the end of file. When Type is omitted the native default is used.

clear

clear()
 

Removes all lines

close

close() : Boolean
 

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

create

create() : Boolean
 
create(FileName) : Boolean
 
Name Type Default Description
FileName String The name of the file to create.

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.

exists

exists() : Boolean
 

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

gotoLine

gotoLine(Line)
 
Name Type Default Description
Line Integer

Changes the value returned by currentLine.

insertLine

insertLine(Line, 
           Pos, 
           Type = typeDefault)
 
Name Type Default Description
Line String The line to insert (without the end-of-line character(s)).
Pos Integer The line position
Type wxTextFileType typeDefault

Insert a line before the line number Pos.

open

open() : Boolean
 
open(Encoding = UTF-8, 
     FileName = "") : Boolean
 
Name Type Default Description
Encoding String UTF-8
FileName String The name of the file to 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. When no encoding is specified the file is opened in UTF-8.

removeLine

removeLine(Line)
 
Name Type Default Description
Line Integer The line to remove.

Removes the line from the file

write

write(Type = wxTextFileType.None, 
      Encoding = UTF-8) : Boolean
 
Name Type Default Description
Type wxTextFileType wxTextFileType.None
Encoding String UTF-8

Change the file on disk. The Type 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.




Design downloaded from Zeroweb.org: Free website templates, layouts, and tools.