C + + writes data to a disk file __c++

Source: Internet
Author: User
/* The disk file is done as follows: (1) A text file is created on the disk, a number of real numbers are stored in the file, (2) A number of real numbers are read in the existing text file on disk, and the maximum, minimum and average values are obtained, and (3) Append records to files, browse text files,
Copy a file to another file (define the class to complete).

* * #include <fstream> #include <iostream>//cerr cannot output using namespace STD After the test is detected;
    Idea: First create the file, then write the file, after the copy file, the final output file class files {public:void build ();
    void Read ();
    void Copy ();
void Write ();

};
    void File::build () {int a[10];
    Ofstream outfile ("file.dat", ios::out);
        if (!outfile) {cerr << "Open error!" << Endl;
    Exit (1);
    } cout << "Please enter 10 integers" << endl;
        for (int i = 0; i < i++) {cin >> a[i];
    OutFile << a[i] << ' t ';
} outfile.close ();
    } void File::read () {int a[20], Max = 0, Min = 0;
    Ifstream infile ("Copy_file.dat", Ios::in | ios::_nocreate);
        if (!infile) {cerr << "Open error!" << Endl;
    Exit (1); for (int i = 0; i < i++) {infile >>
        A[i];
    cout << a[i] << ' t ';
    } cout << Endl;
    Max = a[0];
    Min = a[0];
    int sum = 0, count = 0;
        for (int i = 0; i < i++) {if (Max < a[i]) {max = a[i];
        } if (min > A[i]) {min = a[i];
        Sum + = A[i];
    count++;
    } infile.close ();
    cout << "max=" << Max << Endl;
    cout << "min=" << Min << Endl;
cout << "The average is:" << (Sum/count) << Endl;
    } void File::copy () {int swap[20];
    Ifstream infile ("file.dat", ios::in);
        if (!infile) {cerr << "Open error!" << Endl;
    Exit (1);
    for (int i = 0; i < i++) {infile >> swap[i];
    } infile.close ();
    Ofstream outfile ("Copy_file.dat", Ios::out | ios::_noreplace);
        if (!outfile) {cerr << "Open error!" << Endl;
  Exit (1);  for (int i = 0; i < i++) {outfile << swap[i] << ' \ t ';
} outfile.close ();
    } void File::write () {int a[10];
    Ofstream outfile ("file.dat", Ios::app);
        if (!outfile) {cerr << "Open error!" << Endl;
    Exit (1);
    cout << "Please enter the 10 integers you want to add" << Endl;
        for (int i = 0; i < i++) {cin >> a[i];
    OutFile << a[i] << ' t ';
} outfile.close ();
    int main () {File test; Test.
    Build (); Test.
    Write (); Test.
    Copy (); Test.
    Read ();
    System ("pause");
return 0; }
Second Edition
#include <fstream> #include <iostream> using namespace std;
            Class File {public:void Write (int sign=0) {if (sign==0) {int temp;
            Ofstream outfile ("1.txt", ios::out);
            cout<< "Please enter five integers:" <<endl;
                for (int i=0;i<5;i++) {cin>>temp;
            outfile<<temp<< ' t ';
        } outfile.close ();
            else {int temp;
            Ofstream outfile ("1.txt", Ios::app);
            cout<< "Please enter the five integers you want to add:" <<endl;
                for (int i=0;i<5;i++) {cin>>temp;
            outfile<<temp<< ' t ';
        } outfile.close ();
        } void Read () {int a[10],max,min,count=0;
        float sum=0;
        Ifstream infile ("2.txt", ios::in);
           for (int i=0;i<10;i++) {infile>>a[i]; cout<<a[i]<< ' t ';
        } max=a[0],min=a[0],sum=0;
            for (int i=0;i<10;i++) {if (Max<a[i]) max=a[i];
            if (Min>a[i]) min=a[i];
            Sum+=a[i];
        count++;
    } cout<< "\tmax=" <<Max<< "\tmin=" <<Min<< "\tavr=" <<Sum/count<<endl;
        } void Copy () {int swap[10];
        Ifstream infile ("1.txt", ios::in);
        for (int i=0;i<10;i++) infile>>swap[i];

        Infile.close ();
        Ofstream outfile ("2.txt", ios::out);
        for (int i=0;i<10;i++) outfile<<swap[i]<< ' \ t ';
    Outfile.close ();

}
};
    int main () {File test; Test.
    Write (); Test.
    Write (1); Test.
    Copy (); Test.
    Read ();
return 0; }

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.