In some applications, it is not necessarily required to support, and the record can be saved in the text. At this time, how to add these records to the modified operation? This is provided with a C standard library to handle such issues. This class operates in a behavior unit and sets a record. As long as you get a line of data, you will not be able to use the String class.
head File:/********************************************** *************** * LINEFILE CLASS Interface * * [Author] iori.ogami * [Date] 2005-1-15 * [VER] 1.3 * [ENV] Visual C 6.0 sp5 * *********************************************************** *************** WARNING (Disable: 4786)
#include
Class linefile {public: linefile () {}; linefile (std :: string filename); linefile (const linefile & lf); linefile & operator = (const linefile & lf); virtual ~ linefile ();
Long getLineCount () const {return ln_cnt;}
Std :: string getLinetext (long lnnum) const;
Void Updateline (long lnnum, std :: string txt);
Void insertline (long lnnum, std :: string txt);
Void deleteLine (long lnnum);
Void addline (std :: string txt);
Long getLinebyKeyword (std :: string keyword) const;
Std :: Vector
Protected: std :: fstream * pfile; std :: Vector
CPP file: / ******************************************* IMPLEMATION OF LINEFILE CLASS ****** ***************************************** / # include "linefile.h" #include
#define max_char 1024 // max chars in a line, define your own max_char here
Using namespace std;
/ * * Constructor * / linefile :: linefile (std :: string filename): file_name (filename), ln_cnt (0)
{Pfile = new fstream (file_name.c_str (), iOS_BASE :: Out | iOS_BASE :: App); // Create if not found pfile-> close (); pfile-> open (file_name.c_str (), iOS_BASE :: IN | iOS_BASE :: OUT; // Reopen for Read and Write Char S [MAX_CHAR] = {0}; string ss; while (pfile-> getLine (s, max_char)) {cout << S << endl; ss = S; vec.push_back (ss);} ln_cnt = vec.size (); pfile-> clear (); pfile-> seekg (iOS_BASE :: beg); cout << "construct end. line number: << LN_CNT << endl;}
/ * * Copy Constructor * / Linefile :: Linefile (const linefile & lf): file_name (lf.file_name), VEC (Lf.VEC), LN_CNT (LF.LN_CNT) {Pfile = New FSTREAM (LF.File_name.c_str () , ios_base :: in | iOS_BASE :: Out);
/ * * The destructor * / linefile :: ~ linefile () {if (pfile) {if (pfile-> is_open ()) pfile-> close (); delete pfile;}}
/ * * Move the stream poiter to the start position. If the streout. * / Bool linefile :: reset () {cout << "[linefile" [linefile " :: RESET] Bad Stream "<< Endl; Return False;} IF (pfile-> fail ()) pfile-> clear (); pfile-> seekg (iOS_BASE :: beg); return true;}
/ * * Save the file text from vector to the file :: dump () {// reopen and trunk the file reset (); pfile-> close (); pfile-> open (file_name.c_str (), iOS_BASE :: Out | iOS_BASE :: Trunc;
LN_CNT = vec.size (); for (long (long (long i = 0; i
/ * * * Evaluate Operator '=' * / linefile & linefile :: Operator = (const linefile & lf) {file_name = lf.file_name; ln_cnt = lf.ln_cnt; if (! Vec.empty ()) vec.clear (); vec = LF.VEC; if (pfile) {if (pfile-> is_open ()) pfile-> close (); delete pfile;} pfile = new fstream (lf.file_name.c_str (), iOS_BASE:: IOS_BASE: : OUT;
Return * this;}
/ ***************************** /// the interfaces / ************** ***************** / / * * GET A STRING BY line Number, Note That FeedLine Character '/ n' is not incruded. * / String linefile :: getLinetext (long) Lnnum) const {if (lnnum> ln_cnt || lnnum <= 0) {cout << [linefile :: getLinetext] ILLEGAL line NUMBER: "<< lnnum << Endl; return";
Return vec [lnnum-1];
/ * Insert a line at lnnum, and append a feedline character. * / Void linefile :: insertline (long lnnum, std :: string txt) {if (lnnum> ln_cnt || lnnum <= 0) {cout << " [Linefile :: insertline] Illegal line number: "<< lnnum << endl; return;} vector / * * Update the specifized line with a new string, also append a feedline character. * / Void linefile :: updateline (long lnnum, std :: string txt) {if (lnnum> ln_cnt || lnnum <= 0) {cout << "[linefile :: updateline] ILLEGAL LINE NUMBER:" << lnnum << endl; return; VEC [lnnum-1] = txt; dump ();} / * * Delete a line * / void linefile :: DeleteLine (lnnum> ln_cnt || lnnum <= 0) {cout << [linefile :: deleteLine] Illegal line number: "<< lnnum < Vector / * * Get the line number by a key word, return 0 if not found. * / Long linefile :: getLinebyKeyword (String Keyword) const {for (long i = 0; i / * * Get the line number by a key word. * / Vector