1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
<HTML>
<head><title>wxTextFile</title></head>
<BODY BGCOLOR=#FFFFFF>
<A NAME="wxtextfile"></A><CENTER>
<A HREF="wx.htm"><img align=center src="contents.gif" BORDER=0 ALT="Contents"></A> <A HREF="wx22.htm#classref"><img align=center src="up.gif" BORDER=0 ALT="Up"></A> <A HREF="wx244.htm#wxtextvalidator"><img align=center src="back.gif" BORDER=0 ALT="Previous"></A> <A HREF="wx246.htm#wxthread"><img align=center src="forward.gif" BORDER=0 ALT="Next"></A> </CENTER><HR>
<H2>wxTextFile</H2>
<P>
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).<P>
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.<P>
The typical things you may do with wxTextFile in order are:<P>
<UL>
<LI> Create and open it: this is done with <A HREF="wx245.htm#wxtextfileopen">Open</A>
function which opens the file (name may be specified either as Open argument or
in the constructor), reads its contents in memory and closes it. If all of these
operations are successful, Open() will return TRUE and FALSE on error.
<LI> Work with the lines in the file: this may be done either with "direct
access" functions like <A HREF="wx245.htm#wxtextfilegetlinecount">GetLineCount</A> and
<A HREF="wx245.htm#wxtextfilegetline">GetLine</A> (<I>operator[]</I> does exactly the same
but looks more like array addressing) or with "sequential access" functions
which include <A HREF="wx245.htm#wxtextfilegetfirstline">GetFirstLine</A>/
<A HREF="wx245.htm#wxtextfilegetnextline">GetNextLine</A> and also
<A HREF="wx245.htm#wxtextfilegetlastline">GetLastLine</A>/<A HREF="wx245.htm#wxtextfilegetprevline">GetPrevLine</A>.
For the sequential access functions the current line number is maintained: it is
returned by <A HREF="wx245.htm#wxtextfilegetcurrentline">GetCurrentLine</A> and may be
changed with <A HREF="wx245.htm#wxtextfilegotoline">GoToLine</A>.
<LI> Add/remove lines to the file: <A HREF="wx245.htm#wxtextfileaddline">AddLine</A> and
<A HREF="wx245.htm#wxtextfileinsertline">InsertLine</A> add new lines while
<A HREF="wx245.htm#wxtextfileremoveline">RemoveLine</A> deletes the existing ones.
<LI> Save your changes: notice that the changes you make to the file will <B>
not</B> be saved automatically; calling <A HREF="wx245.htm#wxtextfileclose">Close</A> or doing
nothing discards them! To save the changes you must explicitly call
<A HREF="wx245.htm#wxtextfilewrite">Write</A> - here, you may also change the line
termination type if you wish.
</UL>
<P>
<B><FONT COLOR="#FF0000">Derived from</FONT></B><P>
No base class<P>
<B><FONT COLOR="#FF0000">Include files</FONT></B><P>
<wx/textfile.h><P>
<B><FONT COLOR="#FF0000">Data structures</FONT></B><P>
The following constants identify the line termination type:
<PRE>
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'
};
</PRE>
<B><FONT COLOR="#FF0000">See also</FONT></B><P>
<A HREF="wx88.htm#wxfile">wxFile</A><P>
<B><FONT COLOR="#FF0000">Members</FONT></B><P>
<A HREF="#wxtextfilectordef">wxTextFile::wxTextFile</A><BR>
<A HREF="#wxtextfilector">wxTextFile::wxTextFile</A><BR>
<A HREF="#wxtextfileexists">wxTextFile::Exists</A><BR>
<A HREF="#wxtextfileopen">wxTextFile::Open</A><BR>
<A HREF="#wxtextfileopenname">wxTextFile::Open</A><BR>
<A HREF="#wxtextfileclose">wxTextFile::Close</A><BR>
<A HREF="#wxtextfileisopened">wxTextFile::IsOpened</A><BR>
<A HREF="#wxtextfilegetlinecount">wxTextFile::GetLineCount</A><BR>
<A HREF="#wxtextfilegetline">wxTextFile::GetLine</A><BR>
<A HREF="#wxtextfileoperatorarray">wxTextFile::operator[]</A><BR>
<A HREF="#wxtextfilegetcurrentline">wxTextFile::GetCurrentLine</A><BR>
<A HREF="#wxtextfilegotoline">wxTextFile::GoToLine</A><BR>
<A HREF="#wxtextfileeof">wxTextFile::Eof</A><BR>
<A HREF="#wxtextfilegetfirstline">wxTextFile::GetFirstLine</A><BR>
<A HREF="#wxtextfilegetnextline">wxTextFile::GetNextLine</A><BR>
<A HREF="#wxtextfilegetprevline">wxTextFile::GetPrevLine</A><BR>
<A HREF="#wxtextfilegetlastline">wxTextFile::GetLastLine</A><BR>
<A HREF="#wxtextfilegetlinetype">wxTextFile::GetLineType</A><BR>
<A HREF="#wxtextfileguesstype">wxTextFile::GuessType</A><BR>
<A HREF="#wxtextfilegetname">wxTextFile::GetName</A><BR>
<A HREF="#wxtextfileaddline">wxTextFile::AddLine</A><BR>
<A HREF="#wxtextfileinsertline">wxTextFile::InsertLine</A><BR>
<A HREF="#wxtextfileremoveline">wxTextFile::RemoveLine</A><BR>
<A HREF="#wxtextfilewrite">wxTextFile::Write</A><BR>
<A HREF="#wxtextfilegeteol">wxTextFile::GetEOL</A><BR>
<A HREF="#wxtextfiledtor">wxTextFile::~wxTextFile</A><BR>
<P>
<HR>
<A NAME="wxtextfilectordef"></A>
<H3>wxTextFile::wxTextFile</H3>
<P>
<B></B> <B>wxTextFile</B>() <B>const</B><P>
Default constructor, use Open(string) to initialize the object.<P>
<HR>
<A NAME="wxtextfilector"></A>
<H3>wxTextFile::wxTextFile</H3>
<P>
<B></B> <B>wxTextFile</B>(<B>const wxString& </B><I>strFile</I>) <B>const</B><P>
Constructor does not load the file into memory, use Open() to do it. <P>
<HR>
<A NAME="wxtextfileexists"></A>
<H3>wxTextFile::Exists</H3>
<P>
<B>bool</B> <B>Exists</B>() <B>const</B><P>
Return TRUE if file exists - the name of the file should have been specified
in the constructor before calling Exists().<P>
<HR>
<A NAME="wxtextfileopen"></A>
<H3>wxTextFile::Open</H3>
<P>
<B>bool</B> <B>Open</B>() <B>const</B><P>
Open() opens the file with the name which was given in the <A HREF="wx245.htm#wxtextfilector">constructor</A>
and also loads file in memory on success.<P>
<HR>
<A NAME="wxtextfileopenname"></A>
<H3>wxTextFile::Open</H3>
<P>
<B>bool</B> <B>Open</B>(<B>const wxString& </B><I>strFile</I>) <B>const</B><P>
Same as <A HREF="wx245.htm#wxtextfileopen">Open()</A> but allows to specify the file name
(must be used if the default constructor was used to create the object).<P>
<HR>
<A NAME="wxtextfileclose"></A>
<H3>wxTextFile::Close</H3>
<P>
<B>bool</B> <B>Close</B>() <B>const</B><P>
Closes the file and frees memory, <B>losing all changes</B>. Use <A HREF="wx245.htm#wxtextfilewrite">Write()</A>
if you want to save them.<P>
<HR>
<A NAME="wxtextfileisopened"></A>
<H3>wxTextFile::IsOpened</H3>
<P>
<B>bool</B> <B>IsOpened</B>() <B>const</B><P>
Returns TRUE if the file is currently opened.<P>
<HR>
<A NAME="wxtextfilegetlinecount"></A>
<H3>wxTextFile::GetLineCount</H3>
<P>
<B>size_t</B> <B>GetLineCount</B>() <B>const</B><P>
Get the number of lines in the file.<P>
<HR>
<A NAME="wxtextfilegetline"></A>
<H3>wxTextFile::GetLine</H3>
<P>
<B>wxString&</B> <B>GetLine</B>(<B>size_t </B><I>n</I>) <B>const</B><P>
Retrieves the line number <I>n</I> 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.<P>
<HR>
<A NAME="wxtextfileoperatorarray"></A>
<H3>wxTextFile::operator[]</H3>
<P>
<B>wxString&</B> <B>operator[]</B>(<B>size_t </B><I>n</I>) <B>const</B><P>
The same as <A HREF="wx245.htm#wxtextfilegetline">GetLine</A>.<P>
<HR>
<A NAME="wxtextfilegetcurrentline"></A>
<H3>wxTextFile::GetCurrentLine</H3>
<P>
<B>size_t</B> <B>GetCurrentLine</B>() <B>const</B><P>
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().<P>
<HR>
<A NAME="wxtextfilegotoline"></A>
<H3>wxTextFile::GoToLine</H3>
<P>
<B>void</B> <B>GoToLine</B>(<B>size_t </B><I>n</I>) <B>const</B><P>
Changes the value returned by <A HREF="wx245.htm#wxtextfilegetcurrentline">GetCurrentLine</A>
and used by <A HREF="wx245.htm#wxtextfilegetfirstline">GetFirstLine()</A>/<A HREF="wx245.htm#wxtextfilegetnextline">GetNextLine()</A>.<P>
<HR>
<A NAME="wxtextfileeof"></A>
<H3>wxTextFile::Eof</H3>
<P>
<B>bool</B> <B>Eof</B>() <B>const</B><P>
Returns TRUE if the current line is the last one.<P>
<HR>
<A NAME="wxtextfilegetfirstline"></A>
<H3>wxTextFile::GetFirstLine</H3>
<P>
<B>wxString&</B> <B>GetFirstLine</B>() <B>const</B><P>
This method together with <A HREF="wx245.htm#wxtextfilegetnextline">GetNextLine()</A>
allows more "iterator-like" traversal of the list of lines, i.e. you may
write something like:<P>
<PRE>
for ( str = GetFirstLine(); !Eof(); str = GetNextLine() )
{
// do something with the current line in str
}
</PRE>
<HR>
<A NAME="wxtextfilegetnextline"></A>
<H3>wxTextFile::GetNextLine</H3>
<P>
<B>wxString&</B> <B>GetNextLine</B>()<P>
Gets the next line (see <A HREF="wx245.htm#wxtextfilegetfirstline">GetFirstLine</A> for
the example).<P>
<HR>
<A NAME="wxtextfilegetprevline"></A>
<H3>wxTextFile::GetPrevLine</H3>
<P>
<B>wxString&</B> <B>GetPrevLine</B>()<P>
Gets the previous line in the file.<P>
<HR>
<A NAME="wxtextfilegetlastline"></A>
<H3>wxTextFile::GetLastLine</H3>
<P>
<B>wxString&</B> <B>GetLastLine</B>()<P>
Gets the last line of the file.<P>
<HR>
<A NAME="wxtextfilegetlinetype"></A>
<H3>wxTextFile::GetLineType</H3>
<P>
<B>wxTextFileType</B> <B>GetLineType</B>(<B>size_t </B><I>n</I>) <B>const</B><P>
Get the type of the line (see also <A HREF="wx245.htm#wxtextfilegeteol">GetEOL</A>)<P>
<HR>
<A NAME="wxtextfileguesstype"></A>
<H3>wxTextFile::GuessType</H3>
<P>
<B>wxTextFileType</B> <B>GuessType</B>() <B>const</B><P>
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.<P>
<HR>
<A NAME="wxtextfilegetname"></A>
<H3>wxTextFile::GetName</H3>
<P>
<B>const char*</B> <B>GetName</B>() <B>const</B><P>
Get the name of the file.<P>
<HR>
<A NAME="wxtextfileaddline"></A>
<H3>wxTextFile::AddLine</H3>
<P>
<B>void</B> <B>AddLine</B>(<B>const wxString& </B><I>str</I>, <B>wxTextFileType </B><I>type = typeDefault</I>) <B>const</B><P>
Adds a line to the end of file.<P>
<HR>
<A NAME="wxtextfileinsertline"></A>
<H3>wxTextFile::InsertLine</H3>
<P>
<B>void</B> <B>InsertLine</B>(<B>const wxString& </B><I>str</I>, <B>size_t </B><I>n</I>, <B>wxTextFileType </B><I>type = typeDefault</I>) <B>const</B><P>
Insert a line before the line number <I>n</I>.<P>
<HR>
<A NAME="wxtextfileremoveline"></A>
<H3>wxTextFile::RemoveLine</H3>
<P>
<B>void</B> <B>RemoveLine</B>(<B>size_t </B><I>n</I>) <B>const</B><P>
Delete line number <I>n</I> from the file.<P>
<HR>
<A NAME="wxtextfilewrite"></A>
<H3>wxTextFile::Write</H3>
<P>
<B>bool</B> <B>Write</B>(<B>wxTextFileType </B><I>typeNew = wxTextFileType_None</I>) <B>const</B><P>
Change the file on disk. The <I>typeNew</I> 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.<P>
Returns TRUE if operation succeeded, FALSE if it failed.<P>
<HR>
<A NAME="wxtextfilegeteol"></A>
<H3>wxTextFile::GetEOL</H3>
<P>
<B>static const char*</B> <B>GetEOL</B>(<B>wxTextFileType </B><I>type = typeDefault</I>) <B>const</B><P>
Get the line termination string corresponding to given constant. <I>typeDefault</I> 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.<P>
<HR>
<A NAME="wxtextfiledtor"></A>
<H3>wxTextFile::~wxTextFile</H3>
<P>
<B></B> <B>~wxTextFile</B>() <B>const</B><P>
Destructor does nothing.<P>
</BODY></HTML>
|