`
guoyiqi
  • 浏览: 964768 次
社区版块
存档分类
最新评论

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.

 

分享到:
评论

相关推荐

    C++程序设计教学课件:12 THE C++ INPUT OUTPUT CLASS HIERARCHY.ppt

    C++程序设计教学课件:CHAPTER 12 THE C++ INPUT OUTPUT CLASS HIERARCHY.ppt

    A+B for Input-Output Practice (V)

    Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.

    C++程序设计教学课件:Ch9 Input and output.pdf

    C++程序设计教学课件:Ch9 Input and output.pdf

    独立任务最优调度问题从input.txt提供输入数据,输出结果到output.txt文档。

    问题描述:用2台处理机A和B处理n个作业。设第i个作业交给机器A处理时需要时 3-1独立任务最优调度问题 间a,若由机器B来处理,则需要时间b。由于各作业的特点和机器的性能关系,很可能对于某些i,有a≥b,而对于某些j,ji,...

    简单的词法分析器 可生成一个output文件

    输入语言源代码,生成词法分析结果,一个编译原理的小作业,有执行结果,有源文件代码

    A+B for Input-Output Practice (IV)

    Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be ...

    超清晰PDF C++

    1.3 CONSOLE INPUT/OUTPUT 28 Output Using cout 28 New Lines in Output 29 Tip: End Each Program with \n or endl 30 Formatting for Numbers with a Decimal Point 30 Output with cerr 32 Input Using cin 32 ...

    C++ Basic

    1.3 CONSOLE INPUT/OUTPUT 28 Output Using cout 28 New Lines in Output 29 Tip: End Each Program with \n or endl 30 Formatting for Numbers with a Decimal Point 30 Output with cerr 32 Input Using cin 32 ...

    demonstrate File I/O, user input, and output manipulation

    demonstrate File I/O, user input, and output manipulation Design Consider the following questions: - What input will you prompt from the user? - What potentially bad input could the user enter? - What...

    C++ by Example

    21 Device and Character Input/Output 22 Character, String, and Numeric Functions Contents x VI Arrays and Pointers 23 Introducing Arrays 24 Array Processing 25 Multidimensional Arrays 26 Pointers 27 ...

    C++基础教程完整版

    Input/Output with files C++基础教程简介 怎样使用本教程 读者范围 本教程面向所有希望学习C++语言的读者。如果读者有其他编程语言背景或计算机相关基本知识可以帮助更好的理解教程内容,但这并非必须条件。 对于...

    System Programming with C++

    Learning to Program Console Input / Output A Comprehensive Look at Memory Management Learning to Program File Input / Output A Hands-On Approach to Allocators Programming POSIX Sockets using C++ Time ...

    Qt调用C\C++语言编写的动态链接库示例

    Qt调用C\C++语言编写的动态链接库示例, #ifndef ENGINEMODEL_H #define ENGINEMODEL_H #include "macrodef.h" ...typedef void (__stdcall *ENGINEMODEL)(INPUTDATA *in, OUTPUTDATA *ot); #endif // ENGINEMODEL_H

    潮流计算C++程序

    潮流上机所需要的c++程序,在input.txt中说明了输入格式,在output.txt中输出。教师原件,还给出了说明书。

    Practical C++ Programming C++编程实践

    File Input/Output C++ File I/O Conversion Routines Binary and ASCII Files The End-of-Line Puzzle Binary I/O Buffering Problems Unbuffered I/O Designing File Formats C-Style I/O Routines C-Style ...

    面向对象编程:C++与Java比较教程 英文版

    Chapter 6 - The Primitive Types and Their Input/Output Chapter 7 - Declarations, Definitions, and Initializations Chapter 8 - Object Reference and Memory Allocation Chapter 9 - Functions and Methods ...

    《C++ for Java Programmers》高清完整英文PDF版

    abnormal control flow, input and output, collections: the standard template library, primitive arrays and strings, C-style C++, and using Java and C++: the JNI. For new C++ programmers converted from ...

    Programming with C++

    Chapter 19: Input/Output Streams and Working with Files 453 Chapter 20: Namespaces and Preprocessor Directives 493 Chapter 21: Standard Template Library 509 Chapter 22: Sequence Containers–vector, ...

    最大公约数

    Description 给定任意两个正整数m和n,求出它们的最大公约数。 Input 两个正整数m和n Output m和n的最大公约数 Sample Input 6 9 Sample Output 3

    Record the student exam score

    test the C++ input and output fstream

Global site tag (gtag.js) - Google Analytics