Question

C++ PROGRAM 1. Open the file input called "info.txt" read it and store the data in...

C++ PROGRAM

1. Open the file input called "info.txt" read it and store the data in a vector.

2. Order the data by IP to perform the searches

3. Ask the user for the information search start and end IP's

- Example of data on the info text:

Jun 11 23:26:10 908.93.383.85:4940 Failed password for admin
Aug 2 05:13:39 355.45.117.69:4551 Failed password for root
Oct 24 02:32:14 852.37.168.72:6796 Illegal user

4. Display the records corresponding to those IPs

5. Store the result of the sorting in a file called "sorted.txt"

Homework Answers

Answer #1

Approach

  • Read from info.txt line by line using ifstream and store in a vector
  • sort this vector using the generic sort method from STL library and provide the custom comparator
  • In this custom comparator, we will first extract 4th word from each ip line (which is the ip string) and then compare thw two extracted ip string for sorting.
  • once sorted, we will loop through the vector and write to sorted.txt using outfile and answer to user query to search for ip as well.

Code with inline comments in C++

#include <bits/stdc++.h> 
using namespace std;
string extract_ip(string ip_line){
        stringstream ss(ip_line);
        int count=4;
        string ip;
        while(count--){
                ss>>ip;
        }
        return ip;
}
bool IP_compare(string ip1, string ip2){
        return extract_ip(ip1)<extract_ip(ip2);
}
int main(){
        ifstream infile("info.txt");
        vector<string> allInfo;
        string line;
        //read all info
        while(getline(infile, line)){
                allInfo.push_back(line);
        }

        //sort allInfo using custom comparator
        sort(allInfo.begin(), allInfo.end(), IP_compare);

        //save sorted list in sorted.txt
        ofstream outfile("sorted.txt");
        for(string str:allInfo){
                outfile<<str<<endl;
        }
        string ip;
        do{
                cout<<"Enter Ip for Search or 0 to exit: ";
                
                //take input
                cin>>ip;
                //search for ip in ip_only vector
                bool found=false;
                for(string str:allInfo){
                        if(extract_ip(str)==ip)
                        {
                                cout<<"Found : "<< str<<endl;
                                found=true;
                                break;
                        }
                }
                if(!found)
                        cout<<"Not Found!"<<endl;
        }while(ip!="0");
        
}

Input/Output

info.txt

Jun 11 23:26:10 908.93.383.85:4940 Failed password for admin
Aug 2 05:13:39 355.45.117.69:4551 Failed password for root
Oct 24 02:32:14 852.37.168.72:6796 Illegal user

sorted.txt

Aug 2 05:13:39 355.45.117.69:4551 Failed password for root
Oct 24 02:32:14 852.37.168.72:6796 Illegal user
Jun 11 23:26:10 908.93.383.85:4940 Failed password for admin

Console Input/Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • John wants to buy something for himself with his pocket money. He has a budget of...
    asked 58 seconds ago
  • Provide the following: A list of two functional requirements. A list of two nonfunctional requirements (must...
    asked 8 minutes ago
  • Select a restaurant with which you are familiar and determine the most likely storage management challenge...
    asked 14 minutes ago
  • Proved 3 examples of problems that have surfaced in health and social services from the arrival...
    asked 14 minutes ago
  • A soccer goalie allows a goal on average of 2.3 times per game. Make an estimate...
    asked 14 minutes ago
  • What are some of the similarities and differences associated with caregiver/parental support across these classifications?
    asked 20 minutes ago
  • It is believed that the mean height of middle school students who play basketball on the...
    asked 20 minutes ago
  • Python Indicate whether each statement will evaluate to True or False after running these initializations: a...
    asked 1 hour ago
  • Briefly explain with two examples how traditional leaders in communities can employ the Ubuntu concept for...
    asked 1 hour ago
  • Question 1 Which are two of the four key aspects of hegemony? Group of answer choices...
    asked 2 hours ago
  • A consumer’s preferences are represented by the following utility function: u(x, y) = lnx + 1/2...
    asked 2 hours ago
  • Kingsford Furnishings Company manufactures designer furniture. Kingsford Furnishings uses a job order cost system. Balances on...
    asked 2 hours ago