Example of C ++ reading/writing files and testing program running time

Source: Internet
Author: User

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 ";
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.