Complete examplesCode: Http://zhmster.googlepages.com/ReadFile.rar
Example function introduction:ProgramGoogle. DIC is the dictionary of my Google input method.
Function of this Code Section: sorts the file by word frequency again. Put the high frequency in front.
The Code is a work of my students: I made some modifications.
The Code demonstrates the following technologies:
1. c ++ read and write files
2. Test the running time of a program. The calctime class in the program can be reused. You just need to declare it in the program.
3. Reload the <operator of the str_line class. You can use the sorting function of the linked list to sort data.
# Include <list>
# Include <string>
# Include <iostream>
# Include <fstream>
# Include <algorithm>
# Include <stdio. h>
# Include <time. h>
Using namespace STD;
# Pragma warning (Disable: 4786)
struct str_line
{< br> string str_first;
int int_second;
char str_third [2, 1000];
bool operator <(str_line strline_other)
{< br> If (int_second return false;
else
return true;
}< BR >}strline;
class calctime
{< br> Public:
clock_t start, finish;
double duration;
void showresult ()
{< br> duration = (double) (finish-Start)/clocks_per_sec;
printf ("%. 5f seconds \ n ", duration);
}< br> calctime ()
{< br> Start = clock ();
}< br> ~ Calctime ()
{< br> finish = clock ();
showresult ();
}< BR >};
Void main ()
{
Calctime calc;
Ifstream in_file ("Google. DIC ");
Ofstream out_file ("out.txt ");
List <str_line> save_date_list;
While (in_file> strline. str_first
& In_file> strline.int _ second
& In_file.getline (strline. str_third, 1000 ))
{
Save_date_list.push_back (strline );
}
Save_date_list.sort ();
List <str_line >:: iterator iter = save_date_list.begin ();
For (; iter! = Save_date_list.end (); ITER ++)
{
// Str_line strlinetemp = * ITER;
Out_file <ITER-> str_first <""
<ITER-> int_second <""
<ITER-> str_third <"\ n ";
}
}