Concatenate unique value separated by comma in two string in C++ - c++

CString s;
int res = 0;
char *existing;
char *current;
existing = strtok(urls, ",");
current = strtok(storedurls, ",");
while (existing != NULL)
{
while (current != NULL)
{
res = strcmp(existing, current);
if (res == 0) continue;
s.Append(current);
current = strtok(NULL, ",");
if (current != NULL) s.Append(",");
}
existing = strtok(NULL, ",");
}
strcat(urls, s);
We have a string of storedurl L("url1,url2,url3") and urls L("url3,url5") and I want to store the unique urls from both string and get a single output as L("url1,url2,url3,url5")

Stop using C-style string functions in C++ and get acquainted with the C++ Standard Library.
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
std::vector<std::string> split(std::string const& src, char delim = ',')
{
std::vector<std::string> dst;
std::stringstream ss{ src };
std::string tmp;
while (std::getline(ss, tmp, delim))
dst.push_back(tmp);
return dst;
}
int main()
{
std::string foo{ "url1,url2,url4" };
std::string bar{ "url2,url3,url5" };
auto foo_urls{ split(foo) };
auto bar_urls{ split(bar) };
std::vector<std::string> unique_urls{ foo_urls.begin(), foo_urls.end() };
unique_urls.reserve(foo_urls.size() + bar_urls.size());
unique_urls.insert(unique_urls.end(), bar_urls.begin(), bar_urls.end());
std::sort(unique_urls.begin(), unique_urls.end());
unique_urls.erase(std::unique(unique_urls.begin(), unique_urls.end()), unique_urls.end());
for (auto const& url : unique_urls)
std::cout << url << '\n';
}

DWORD res = 0;
CString existingUrls = urls;
CString curUrls;
CString curItemInExitingUrl;
CString curItemInCurUrl;
CString urlsToAdd;
while (!existingUrls.IsEmpty()) {
CWinLogFile::GetTok(existingUrls, _T(","), curItemInExitingUrl);
curUrls = src.urls;
while (!curUrls.IsEmpty()) {
CWinLogFile::GetTok(curUrls, _T(","), curItemInCurUrl);
if (curItemInExitingUrl.Compare(curItemInCurUrl)==0)
{
res = 1;
}
}
if (res == 0)
{
urlsToAdd.Append(curItemInExitingUrl);
urlsToAdd.AppendChar(',');
}
else
res = 0;
}
urlsToAdd.Append(src.urls);
Found the solution for my posted question. Where urls & src.urls are two comma separated values of urls. Thanks.

Related

How to read file into class in C++?

I am reading File that contains
Boss, level, Specials,
"FireMen", 3, "Flame,Thrower", Fire,Breath
"Medusa", 6, "Snake,Poison"
"Hype", 10, "One,punch,Knock", Fly,Kick, "Dragon,Breath"
I am trying to read it into class with objects boss, level and specials
I am having problem reading from file since I split each by words by comma but it read specials like Flame,Thrower as separate due to comma in between them. How can I combine specials rather then splitting by commas so that it read Flame Thrower rather then Flame and thrower separately.
Also some specials are in quotation others are not.
I have been stuck at reading this rather complicated file. If anyone has time to read through my code and fix 10s of errors that I am getting would greatly be appreciated, Thank You
(What I did doesn't make much sense since this is my one month into C++ so still newbie and progressing)
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include "Boss.h"
using namespace std;
vector <Boss*> Info;
Boss* parseLine(string str)
{
vector<string> store;
string smt = " ";
Boss* values = nullptr;
if (smt != "")
{
(store.push_back(smt));
}
for (int i = 0; i < (int)str.size(); i++)
{
char ch = str[i];
if (ch != ',')
{
smt = smt + ch;
}
else
{
store.push_back(smt);
smt = "";
}
}
values = new Boss(store[0], stoi(store[1]), store[2]);
Name = store[0];
Level = store[1];
Specials = store[2];
return values;
}
bool readFile()
{
std::ifstream myFile("Bosses.txt");
if (!myFile.is_open())
{
cout << "fAILED" << "\n";
return false;
}
string str;
int i = 0;
while (std::getline(myFile, str))
{
cout << str << endl;
if (str[0] != '/')
{
Boss* Boss = parseLine(str);
result.push_back(Boss);
}
}
return true;
}
int main()
{
std::cout << "Read file\n;";
bool data = readFile();
for (Boss* t : result)
{
delete t;
}
And Class
#include <string>
#include <vector>
class Boss {
std::string Name;
int Level;
std::vector<std::string> Specials;
Boss(std::string Name, int Level, std::vector<std::string> Specials);
~Boss();
Boss(Boss& b);
void setName(std::string Name);
void setLevel(int Level);
};
Boss::Boss(std::string Name, int Level, std::vector<std::string> Specials)
{
this->Name= Name;
this->Level = Level;
this->Specials = Specials;
}
Boss::~Boss() {}
Boss::Boss(Boss& b)
{
Name = b.Name;
Level = b.Level;
Specials = b.Specials;
}
void Boss::setName(std::string Name) {
this->Name = Name;
}
void Boss::setLevel(int Level)
{
this->Level = Level;
}
This code reads your file and stores the data in the std::vector Info using stringstreams and other things to parse.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
class Boss {
std::string Name;
int Level;
std::vector<std::string> Specials;
public:
Boss(std::string n, int l, std::vector<std::string> s)
{
this->Name = n;
this->Level = l;
this->Specials = s;
};
~Boss() {};
Boss(const Boss& b) {
this->Name = b.Name;
this->Level = b.Level;
this->Specials = b.Specials;
};
Boss() {};
Boss operator= (Boss b) {
this->Name = b.Name;
this->Level = b.Level;
this->Specials = b.Specials;
return b;
}
void setName(std::string n) { Name = n; };
void setLevel(int l) { Level = l; };
void setSpecials(std::vector<std::string> s) {Specials = s;}
std::string getName() const { return Name; }
int getLevel() const { return Level; }
std::vector<std::string> getSpecials() const { return Specials; }
};
vector <Boss> Info;
Boss parseLine(string str)
{
vector<string> store;
string smt;
stringstream evaluator;
Boss value;
evaluator << str; //put our string into evaluator
{
char double_quote_remover;
evaluator >> double_quote_remover; //remove the first double quote
getline(evaluator, smt, '"'); //get the name until the next double quote
char comma_remover;
evaluator >> comma_remover; //remove the comma
}
value.setName(smt); //put smt into the Boss's name
{
int Level;
evaluator >> Level; //get an int from our string
value.setLevel(Level); //put our int into our Boss
}
char curr;
while (evaluator >> curr && curr != ','); //get the comma after the int
smt = ""; //reset
curr = ' '; //reset
while (evaluator >> curr) { //while we read chars into curr
if (isalpha(curr)) { evaluator.putback(curr); curr = ','; } //if the char is a letter, put it back
if (curr == '\"') getline(evaluator,smt,'\"'); //if the char is a double quote, read until we reach another
else if (curr == ',') getline(evaluator,smt,','); //if the char is a comma, read until we get another
if (smt != "") //if the string is not null, put it in store
store.push_back(smt);
smt = ""; //reset
}
value.setSpecials(store); //put store into our Boss
return value; //return our boss
}
bool readFile()
{
std::ifstream myFile("Bosses.txt");
if (!myFile.is_open())
{
cout << "FAILED" << "\n";
return false;
}
string str;
getline(myFile, str); //for the first line, which we don't need
int i = 0;
while (std::getline(myFile, str))
{
Boss Boss = parseLine(str);
Info.push_back(Boss);
}
return true;
}
int main()
{
bool data = readFile();
}
This should work. I have tested it thouroughly. Inform me of any errors.
Note: There was no need for pointers that were in your original code.

How to take a function using stringstream to parse the numbers, and put them into an array?

So I am using stringstream in a function in c++ to take the numbers from a string, and then return the numbers to an array in main, but for some reason, they always return as 0 instead of the actual numbers. Code is below, does anyone know how to fix this?
int main()
{
for(int i = 0; i < userInput.length(); i++)
{
...
else
{
chemNumbers[i] = extractIntegerWords(userInput);
cout << chemNumbers[i] << endl;
}
}
int extractIntegerWords(string str)
{
stringstream ss;
int num = 0;
ss << str;
/* Running loop till the end of the stream */
string temp;
int found;
if(!ss.eof())
{
ss >> temp;
if (stringstream(temp) >> found)
{
num = found;
}
temp = "";
}
return found;
}
The original if statement doesn't pertain to the function only what is seen in the else statement, which is in main
This is off the top of my head and I don't have much time to test it, but this should put you in the right direction:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::vector;
inline bool tryGetInt(const string& str, string& out) { istringstream sStream(str); return !(sStream >> out).fail(); } /*!< Tries to parse a string to int. */
void splitString(const string &str, const string &delimiters, vector<string> &tokens) {
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) {
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
int32_t main(int argCount, char* argValues[]) {
for (int32_t i = 0; i < argCount; i++) {
cout << "Found argument " << argValues[i] << endl;
auto foundIntegers = getIntegerFromString(string(argValues[i]));
// Do whatever
}
return 0;
}
vector<int64_t> getIntegerFromString(string formula) {
vector<int64_t> foundInts;
string temp;
if (tryGetInt(formula, temp)) {
vector<string> intsAsStrings;
splitString(temp, " ", intsAsStrings);
for (auto item : intsAsStrings) {
foundInts.push_back(stol(item));
}
}
return foundInts;
}
Regular expression might help:
std::vector<std::pair<std::string, std::size_t>> Extract(const std::string& s)
{
std::vector<std::pair<std::string, std::size_t>> res;
const std::regex reg{R"(([a-zA-Z]+)(\d*))"};
for (auto it = std::sregex_iterator(s.begin(), s.end(), reg);
it != std::sregex_iterator();
++it)
{
auto m = *it;
res.push_back({m[1], m[2].length() == 0 ? 1 : std::stoi(m[2])});
}
return res;
}
int main()
{
for (auto p : Extract("C6H12O6")) {
std::cout << p.first << ": " << p.second << std::endl;
}
}
Demo
I add option for array instead vector for #I'mbadatCS
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
vector<int> extractIntegerWords(const string& _str)
//for array option: void extractIntegerWords(const string& _str, int* numArr)
{
stringstream ss;
ss << _str;
vector<int> vec;
string temp;
int found;
//for array option: int idx = 0;
while(!ss.eof())
{
ss >> temp;
if (stringstream(temp) >> found)
{
vec.push_back(found);
//for array option:numArr[idx++] = found;
}
temp = "";
}
return vec;
//for array option: return;
}
int main()
{
string s = "bla 66 bla 9999 !"
vector<int> res = extractIntegerWords(s);
for (int i = 0; i < res.size(); ++i)
{
cout << res[i] << ", " << endl;
}
}
a few comments:
const string& in input argument, not string. you don't want harm your source data
you need a loop not "if" (like ths the operation will run just one time)
where your array??
in C++ prefer use vector and not array

why whould this while loop won't stop? c++

This is a code for reading in .txt file and spliting it by : and printing out the result. But I got stuck on the while-loop. This is my code.
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
string* split(const string& str, const string& delim) {
string* string_list = new string[10];
int idx = 0;
char *token = strtok(const_cast<char*>(str.c_str()), delim.c_str());
while (token != NULL) {
string_list[idx] = token;
token = strtok(NULL, delim.c_str());
++idx;
}
return string_list;
}
struct Item {
string name;
string age;
string id;
string subject[10];
};
struct Item* create_item() {
struct Item* pitem = new Item;
return pitem;
};
void insert_item(struct Item *prev_item, struct Item *item) {
item = (prev_item + 5);
}
int main() {
string student, student2;
string *string_list, *subject, *string_list2, *subject2;
struct Item* pstudent, *pstudent2;
ifstream fin;
fin.open("input.txt");
fin >> student;
while (student != "\n") {
string_list = split(student, ":");
pstudent = create_item();
pstudent->name = *string_list;
pstudent->age = *(string_list + 1);
pstudent->id = *(string_list + 2);
subject = split(*(string_list + 3), ",");
for (int i = 0; i < 10; i++) {
if (*(subject + i) != "") {
pstudent->subject[i] = *(subject + i);
}
}
cout << *(string_list+1) << endl;
fin >> student2;
string_list = split(student2, ":");
pstudent2 = create_item();
insert_item(pstudent, pstudent2);
pstudent2->name = *(string_list);
pstudent2->age = *(string_list + 1);
pstudent2->id = *(string_list + 2);
subject2 = split(*(string_list + 3), ",");
for (int i = 0; i < 10; i++) {
if (*(subject2 + i) != "") {
pstudent2->subject[i] = *(subject2 + i);
}
}
}
cout << pstudent2->name << endl;
fin.close();
return 0;
}
I am still working on this code, but the while-loop in the main() will not stop. I wanted it to stop when the input.txt file input is a new line.
input.txt is
Mary:20:287:Math,Algorithm\n
Tom:21:202:Math,English\n
Hee:20:256:Math
thanks in advance!
Don't try to process more than one student in the body of the loop.
Rather than trying to write a linked-list type, use an existing collection type.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using strings_view = std::vector<std::string_view>;
strings_view split(std::string_view str, char delim) {
strings_view view;
while (str.size())
{
auto pos = str.find(delim);
view.push_back(str.substr(0, pos - 1));
str = str.substr(pos + 1);
}
return view;
}
struct Item {
std::string name;
std::string age;
std::string id;
std::vector<std::string> subject;
};
int main() {
std::vector<Item> students;
std::ifstream fin("input.txt");
for (std::string line; get_line(fin, line);) {
strings_view view = split(line, ':');
Item student;
student.name = view[0];
student.age = view[1];
student.id = view[2];
string_view subject = split(view[3], ',')
student.subject.assign(subject.begin(), subject.end());
students.push_back(student);
}
std::cout << students[1].name << std::endl;
return 0;
}

How to split char pointer with multiple delimiters & return array of char pointers in c++?

In the duplicate of this question Split char* to char * Array it is advised to use string rather than char*. But I need to work with LPWSTR. Since it's a typedef of char*, I prefer to use char*. I tried with the following code, which gives the wrong output:
char**splitByMultipleDelimiters(char*ori,char deli[],int lengthOfDelimiterArray)
{
char*copy = ori;
char** strArray = new char*[10];
int j = 0;
int offset = 0;
char*word = (char*)malloc(50);
int length;
int split = 0;
for(int i = 0; i < (int)strlen(ori); i++)
{
for(int k = 0; (k < lengthOfDelimiterArray) && (split == 0);k++)
{
if(ori[i] == deli[k])
{
split = 1;
}
}
if(split == 1)//ori[i] == deli[0]
{
length = i - offset;
strncpy(word,copy,length);
word[length] = '\0';
strArray[j] = word;
copy = ori + i + 1;
//cout << "copy: " << copy << endl;
//cout << strArray[j] << endl;
j++;
offset = i + 1;
split = 0;
}
}
strArray[j] = copy;
// string strArrayToReturn[j+1];
for(int i = 0; i < j+1; i++)
{
//strArrayToReturn[i] = strArray[i];
cout << strArray[i] << endl;
}
return strArray;
}
void main()
{
char*ori = "This:is\nmy:tst?why I hate";
char deli[] = {':','?',' ','\n'};
int lengthOfDelimiterArray = (sizeof(deli)/sizeof(*deli));
splitByMultipleDelimiters(ori,deli,lengthOfDelimiterArray);
}
Are there any other ways to split LPWSTR?
Wait, what are you talking about? I don't see LPWSTR anywhere in your code. Are you trying to convert to LPWSTR? If so, there's a standard library function for that. There's also a standard library-based solution for splitting over multiple chars. So all together, your code might look like this:
#include <codecvt>
#include <cstdio>
#include <locale>
#include <sstream>
#include <string>
using std::string;
using std::wstring;
wstring toWide(const string &original)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(narrow_utf8_source_string);
}
std::vector<wstring> splitMany(const string &original, const string &delimiters)
{
std::stringstream stream(original);
std::string line;
while (std::getline(original, line))
{
std::size_t prev = 0, pos;
while ((pos = line.find_first_of(delimeters, prev)) != std::string::npos)
{
if (pos > prev)
wordVector.push_back(line.substr(prev, pos-prev));
prev = pos + 1;
}
if (prev < line.length())
wordVector.push_back(line.substr(prev, std::string::npos));
}
}
int main()
{
string original = "This:is\nmy:tst?why I hate";
string separators = ":? \n"
std::vector<wstring> results = splitMany(original, separators);
}
This code uses the standard library for these functions and is much less error-prone than doing it manually.
Good luck!
Edit: To be clear, wstring == LPWSTR == wchar_t*.
Edit 2: To convert a string to a wstring:
#include <codecvt>
#include <locale>
#include <string>
using std::string;
using std::wstring;
string toMultiByte(const wstring &original)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes(original);
}

Make 2D Array by Splitting a text with a delimiter C++

Well I have that code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string s = "0,0,0,1,1,1,0,0,1";
std::string delimiter = ",";
int x = 0;
std::string mapa[9];
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
mapa[x] = token;
x++;
}
std::cout << s << std::endl;
cin.get();
}
Parse (split) a string in C++ using string delimiter (standard C++)
I have x array, but I need a second dimension the Y... I get the content from a text file called map.txt:
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
And I need to split it by commas (for the x) and later by newlines (for y)...
But Idk how to do the Y array... What Can I do?
Thanks!
You may read the lines from the file as
fstream fstr;
fstr.open("file.txt",ios::in);
string str;
int yDimension = 0;
while(getline(fstr,str)
{
yDimension++; //do appropriate thing with the y dimension
std::string token;
while ((pos = str.find(delimiter)) != std::string::npos) {
token = str.substr(0, pos);
std::cout << token << std::endl;
str.erase(0, pos + delimiter.length());
mapa[x] = token;
x++;
}
}
You can read the entire file of any number of rows with any number of comma-delimited columns (memory-permitting) with this:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <vector>
struct int_reader : std::ctype<char>
{
int_reader() : std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table()
{
static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());
rc[','] = std::ctype_base::space;
rc['\n'] = std::ctype_base::space;
return &rc[0];
}
};
int main()
{
std::vector<std::vector<int> > myFileData;
std::ifstream fin("MyDataFile.txt", std::ifstream::in);
std::string buffer;
while (std::getline(fin, buffer))
{
std::stringstream ss(buffer);
std::vector<int> t;
int_reader reader;
ss.imbue(std::locale(std::locale(), &reader));
std::copy(std::istream_iterator<int>(ss), std::istream_iterator<int>(), std::back_inserter(t));
myFileData.push_back(t);
}
// do whatever you need to with the loaded arrays ...
return 0;
}
You can use ifstream::getline(), with a delimiter ','
ifstream file ( "map.txt" );
string value;
while ( file.good() )
{
getline ( file, value, ',' );
}
Or you can read all the text and use regular expression to take each text out between delimiter.
The sortest way to do that without vectors:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
fstream fstr;
fstr.open("mapa.txt");
char mapa[31][9];
int x = 0, y = 0;
char c;
while(fstr.good())
{
c = fstr.get();
if (c!= ',') {
mapa[x][y] = c;
x++;
}
if (c=='\n')
{
x = 0;
y++;
}
}
fstr.close();
cin.get();
}
Only 32 lines!! :D