-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode3.cpp
More file actions
62 lines (50 loc) · 1.67 KB
/
code3.cpp
File metadata and controls
62 lines (50 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
void processLog(const std::string& filename) {
// Prefix "bytes_" to the filename to create the output file name
std::string outputFilename = "bytes_" + filename;
// Open the input log file
std::ifstream inputFile(filename);
if (!inputFile.is_open()) {
std::cerr << "Error: Unable to open input file." << std::endl;
return;
}
// Initialize counters
int numLargeResponses = 0;
long long totalBytesLargeResponses = 0;
// Open the output file
std::ofstream outputFile(outputFilename);
if (!outputFile.is_open()) {
std::cerr << "Error: Unable to create output file." << std::endl;
inputFile.close();
return;
}
// Read and process each line in the input file
std::string line;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::vector<std::string> tokens(istream_iterator<string>{iss}, istream_iterator<string>());
// Extract the response size (bytes) from the log record
long long bytesSent = std::stoll(tokens.back());
// Check if response size is larger than 5000 bytes
if (bytesSent > 5000) {
numLargeResponses++;
totalBytesLargeResponses += bytesSent;
}
}
// Write results to the output file
outputFile << numLargeResponses << '\n';
outputFile << totalBytesLargeResponses << '\n';
// Close files
inputFile.close();
outputFile.close();
}
int main() {
// Example usage
std::string filename = "hosts_access_log_00.txt";
processLog(filename);
return 0;
}