C + + Job 2-File stream object __c++

Source: Internet
Author: User
Job RequirementsGenerate random Data file text.txt. Sort after reading data from the file text.txt. Writes the sorted data to the In.txt file. Resolves a naming conflict problem when loading multiple-head files in a main program file. SolutionGenerate data by random generating function to write to the file stream, then read the file stream, speak the data in num[], sort, write the data in num[] to the new file, not open the STD namespace globally, use Std::xxx, and so on when calling. Code main.cpp
//  main.cpp
//  F-WORK2////
Created by Zyj on  2017/3/14.  copyright©2017 year Zyj. All rights reserved.

#include <iostream>
#include <string>
#include "file.hpp"

int main (int argc, const char * argv[])
{
    int n;
    Std::cin >> N;
    int *num = new Int[n];

    std::string text = "Text.txt";  
    std::string in = "In.txt";

    File F = file (text, n, num);

    F.randdate ();
    F.sortdate ();

    File F_ = file (in, n, num);

    F_.coutdate ();

    return 0;
}
file.hpp
//  file.hpp
//  F-WORK2////
Created by Zyj on  2017/3/14.  copyright©2017 year Zyj. All rights reserved.

#ifndef file_hpp
#define FILE_HPP

#include <stdio.h>
#include <string>
# Include <fstream>

class File
{
    std::string fileName;  filename
    int n;  data Volume
    int *num;

Public:
    File (std::string filename_, int n_, int *num_): FileName (Filename_), N (n_), num (num_) {}

    void Randdate ();  generate random data file filename
    void sortdate ();  read the random sequence in file filename and sort it into num
    void coutdate ();  store data in num in the filename file

    int random (double St, double ed);  generate st~ed random number
};

#endif/* FILE_HPP * *
File.cpp
File.cpp//F-WORK2////Created by Zyj on 2017/3/14. COPYRIGHT©2017 year Zyj.
All rights reserved. #include "file.hpp" #include <algorithm> #include <ctime> #include <cstdlib> #define S 0 #define T

    1000000//Generate random data file void File::randdate () {std::fstream fp (fileName, std::ios::out);
    while (!FP) {Fp.open (fileName, std::ios::out);

    } srand (Unsigned (time (0)));
    int k = n;
    while (k--) {FP << random (S, T) << ' \ n ';
} fp.close ();

    //Read data file, save num, and sort void file::sortdate () {std::fstream fp (fileName, std::ios::in);
    while (!FP) {Fp.open (fileName, std::ios::in);
    int k = 0;

    while (FP >> num[k++]) {} std::sort (num, num + N);
Fp.close ();

    ///Save data in num in file void File::coutdate () {std::fstream fp (fileName, std::ios::out);
    while (!FP) {Fp.open (fileName, std::ios::out);
   for (int i = 0; i < n; i++) {fp << num[i] << ' \ n ';
} fp.close (); //Generate random number, random number within st~ed int file::random (double St, double ed) {return (int) St + (ed-st) * rand ()/(Rand_max +
1.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.