zoukankan      html  css  js  c++  java
  • c++ input,output

    You should be comfortable with the content in the modules up to and including the module "Input Output" for this project.

    You must follow the style guidefor all projects.

    For this project, download the text file weblog.txt
    Note: To download this file, right click on the link and select SAVE AS

    This file is an Apache web log taken from the web server for St. Mary's University. When a visitor goes to their web site, the visitor's browser makes a request to the web server to view a web page, which is a file residing on the server. Each time a page is requested by a browser, the web server records information about that request. This weblog.txt holds the details of some of those requests. See below for a list of fields with an example:

    Web Log Example

    This file does not include all possible information that could be collected by a web server. The full description of the apache log file format can be found here: http://httpd.apache.org/docs/2.2/logs.html

    For this project, you can use each line of the web log file as one string using the string class' getline function.

    Minimum Requirements:

    • Create a non-member function to read each line of the web log file (4 points).
    • Each line should then be stored in a vector such that the first element of the vector is the first line of the web log file. Because each element will hold an entire line from the file, the vector should be declared as a vector of strings (4 points).  Note: You must declare the vector in a function.
    • Create another non-member function to sort the contents of the vector. Make sure to pass the vector by reference or your sort will dissappear when the function ends! (4 points).
    • Create one more non-member function to write the contents of the vector to a file. Each element should be on its own line in the new file. The contents of the new file should look like the original input file once your program completes, but in sorted order (4 points).
    • Create a main function that calls all of your non-member functions (4 points).

    Answer

    #include<iostream>
    #include<sstream>
    #include<fstream>
    #include<vector>
    #include<algorithm>

    using namespace std;

    木其工作室
    void readFile(vector<string>& myVector);
    void outFile(vector<string>& myVector);
    void sortVector(vector<string>& myVector);

    int main()
    {
     vector<string> myVector;

     readFile(myVector);
     sortVector(myVector);
     outFile(myVector);

     system("pause");

     return 0;
    }

    void readFile(vector<string>& myVector)
    {

     string line;
     ifstream myfile ("weblog.txt");

     if (myfile.is_open())
     {
      while( getline(myfile, line) )
      {
       myVector.push_back(line);
      }

      myfile.close();
      cout<<"File Read successfully."<<endl;
     }
     else
     {
      cout<<"Error"<<endl;
     }

    }

    void outFile(vector<string>& myVector)
    {
      //writing to a file
      ofstream outFile;
      outFile.open("weblogSorted.txt");
      for(unsigned int i = 0; i<myVector.size(); i++)
      {
       outFile<<myVector[i]<<std::endl;
      }
      outFile.close();
      cout<<"Sorted elements written to a file."<<endl;
    }

    void sortVector(vector<string>& myVector)
    {
     //sorting vector
      std::sort(myVector.begin(), myVector.end());
      cout<<"Vector sorted successfully successfully."<<endl;
    }

    No attachments uploaded.

  • 相关阅读:
    【BZOJ 1185】 凸包+旋转卡壳
    【BZOJ 2829】 2829: 信用卡凸包 (凸包)
    【BZOJ 1045】 1045: [HAOI2008] 糖果传递
    【BZOJ 2453|bzoj 2120】 2453: 维护队列 (分块+二分)
    【BZOJ 3343 】 分块
    【BZOJ 1069】 凸包+旋转卡壳
    【NOIP 2016 总结】
    【无聊放个模板系列】洛谷 负环 模板
    【无聊放个模板系列】BZOJ 3172 (AC自动机)
    【无聊放个模板系列】HDU 3506 (四边形不等式优化DP-经典石子合并问题[环形])
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3889301.html
Copyright © 2011-2022 走看看