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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#include <ios>
#include <map>
//lets assume this file: advertlinks.txt is where links are stored.
//also assuming the links are stored in this format:
//category_of_link, delimiter_char, link_to_advert, new_line_char:
//Fashion | https:\\www.thisfashionadvert
//HomeStuff | https:\\www.thishomestuffadv
//tools | https:\\www.thistoolsadv..
//Technology | https:\\www.techadvlinks...
//Fashion | https:\\www.thisfashionadvert2
//Fashion | https:\\www.thisfashionadvert3
//Beauty | https:\\www.thisbeautyadvert1
//Technology | https:\\www.techadvlinks1
//...
std::pair<std::string,std::string> split_str(const std::string& str, char delim = '|')
{
std::string category{},link{};
int delim_pos = str.find(delim);
category = std::string(str.begin(), str.begin() + delim_pos);
link = std::string(str.begin()+delim_pos+1, str.end());
return {category,link};
}
//returns a pair of status flag(true if read was successful) and the map that contains the data from the file containing links;
std::pair<bool,std::map<std::string,std::vector<std::string>>> read_main_adv_file(const std::string& file)
{
std::ifstream file_stream{file};
std::map<std::string,std::vector<std::string>> raw_links{};//category & link
if(file_stream)
{
std::string data{};
while(std::getline(file_stream,data,'\n'))
{
auto [category,link] = split_str(data);
raw_links[category].push_back(link);
}
return {true,raw_links};
}
std::cout<<"failed to open: "+file+".txt, try again\n";
return {false,raw_links};
}//file auto closed
//writes data to a category file after selection
bool write_to_category_file(const std::string& file_name,const std::vector<std::string>& link_data)
{
std::ofstream write_stream{file_name,std::ios::app};
if(write_stream)//if file opened successfully
{
for(const auto& link : link_data) //you should check if the links already exist in this file since you're appending, I didn't do it here
{
write_stream<<link<<"\n";
}
return true; //data written
}
return false;//write failed
}//file auto closed
void categorize_links()
{
std::map<std::string,std::string> category_file_names{
{"Fashion","fashion_links.txt"},
{"Tools","Tools_links.txt"},
{"Technology","Tech_links.txt"}
};//file names stored statically.
auto [status,link_data] = read_main_adv_file("advertlinks.txt");
if(status == true)//successfully read the data
{
for(const auto& category : link_data)//loop over the map keys
{
auto file_name = category_file_names[category];
auto category_links = link_data[category];
bool write_complete = write_to_category_file(file_name,category_links);
if(write_complete)
{
std::cout<<"data written successfully to : " +category + " file";
}
else
std::cout<<"failed to write data to : " + category + " file";//handle this error.
}
}
else{
//handle this error
}
}
int main()
{
categorize_links();
}
| |