How to split string using CRLF delimiter in cpp? - c++

I have some string :
testing testing
test2test2
these lines are devided by CRLF. I saw that there are : 0d0a0d0a deviding them.
How can I split it using this information?
I wanted to use str.find(CRLF-DELIMITER) but can't semm to figure how
editing :
I already used str.find("textDelimiter"), but now I need it to look for hexa and not search for a string "0d0a0d0a"

Use boost::split to do that. Please also take a look at Boost.Tokenizer.
Here is another way of doing it using regex:
using std::endl;
using std::cout;
using std::string;
using std::vector;
using boost::algorithm::split_regex;
int main()
{
vector<string> res;
string input = "test1\r\ntest2\r\ntest3";
split_regex(res, input, boost::regex("(\r\n)+"));
for (auto& tok : res)
{
std::cout << "Token: " << tok << std::endl;
}
return 0;
}
Here is the way of doing it without Boost:
#include <string>
#include <sstream>
#include <istream>
#include <vector>
#include <iostream>
int main()
{
std::string strlist("line1\r\nLine2\r\nLine3\r\n");
std::istringstream MyStream(strlist);
std::vector<std::string> v;
std::string s;
while (std::getline(MyStream, s))
{
v.push_back(s);
std::cout << s << std::endl;
}
return 0;
}

Related

Store a column from csv excel file to a vector double variable C++

I have a csv file in Excel that has a column of double data. I am trying to read that column and store the values in a vector variable using a while loop. I tried to use getline and then convert them into a double using stod.
The column has more values, but this is how the csv file looks like:
A
B
51.32
53.62
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream myFile("CData.csv");
int i= 0;
string val;
std::vector<double> y;
while (getline(myFile, val, ',')) {
y.push_back(stod(val));
cout << "test: " << y.at(i);
i++;
}
return 0;
}
I'm not sure what I'm doing wrong. Somehow, there is no output on the console app. I tried it with string ang it worked but when I try to convert to a double, it doesn't. Is there another way to do this, or did I miss something in the code? Thanks, I'm new to coding.
You need to first parse the line and then look at the line for comma separated data. Also you need to chek if data is digits or not.
Here is an example:
#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
int main()
{
ifstream myFile("CData.csv", std::ios::in);
int i= 0;
string val;
std::string line;
std::vector<double> y;
while (getline(myFile, line))
{
std::stringstream sstr(line);
while (getline(sstr, val, ','))
{
bool flag = true;
for (auto c : val)
if (!isdigit(c))
flag = false;
if (flag){
y.push_back( std::stod(val));
std::cout << "number: " << y.at(i) << std::endl;
i++;
}
else std::cout << "not number: " << val << std::endl;
}
}
return 0;
}
Good luck!

sort in lexicographic order in map C++

I am using STL map in C++ for counting the frequency of words in a text file and words must be sort in lexicographic order. Input data is given as a text file. Ive already read and added them in map but i got a problem with sorting.
Example, i have { "Abc", "abc", "bag", "Boom", "great"}. When i added them in map, i got
Abc 1 Boom 1 abc 1 bag 1 great 1
but expected result is
Abc 1 abc 1 Boom 1 bag 1 great 1
#include <iostream>
#include <cstring>
#include <map>
#include <fstream>
using namespace std;
typedef map<string, int> word_count;
int main(){
word_count wc;
fstream f_in;
f_in.open("test.in");
string x;
while( !f_in.eof()){
f_in >> x;
wc[x]++;
}
f_in.close();
return 0;
}
Here is my code for reading input. Any help for my problem? Thanks
The OP wants a custom sort order that's subtly different from the standard lexicographical order. A map with a custom sort order can be achieved by passing in a custom Compare (Compare is the third template parameter of map):
#include <algorithm>
#include <cctype>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <vector>
using std::string;
using std::transform;
using std::map;
using std::cout;
struct Compare {
bool operator() (const string& s0, const string& s1) const {
// construct all lowercase versions of s0 and s1
string str0(s0.length(),' ');
string str1(s1.length(),' ');
transform(s0.begin(), s0.end(), str0.begin(), tolower);
transform(s1.begin(), s1.end(), str1.begin(), tolower);
if (!str0.empty() and !str1.empty() and str0.front()==str1.front()) {
// do a standard lexicographic sort if the first character is the same
return s0 < s1;
}
else {
// otherwise, do a case-insensitive lexicographic sort using the lowercased strings
return str0 < str1;
}
}
};
typedef map<string, int, Compare> word_count;
int main(){
word_count wc;
auto words = { "Abc", "abc", "bag", "Boom", "great"};
for (auto word : words)
wc[word]++;
for(auto elem : wc)
cout << elem.first << " " << elem.second << '\n';
return 0;
}
This indeed produces the desired output:
Abc 1
abc 1
Boom 1
bag 1
great 1
Try out a live version of the code online
By default, the third template parameter of a map is less<key> (in this case, less<string>), which will sort strings in the standard lexicographical A-z order.
Here is a complete example with file reading included, and using the base sorting functionality of std::map.
#include <iostream>
#include <cstring>
#include <map>
#include <fstream>
typedef std::map<std::string, int> word_count;
int main(int argc, char** argv){
if(argc < 2){
std::cout << "Please provide a file name." << std::endl;
return 1;
}
word_count wc;
std::ifstream inputfile(argv[1]);
if (inputfile.is_open()){
std::string x;
while(inputfile >> x){
wc[x]++;
}
inputfile.close();
}else {std::cout << "Program aborted: unable to open input file" << std::endl; return 1;}
for(auto word: wc){
std::cout << word.first << "\t" << word.second << std::endl;
}
return 0;
}

Text Parser c++ code

I need a C++ code for the following problem:
i have a text file that i want to start reading from a specific line, then i need to print the output located between the characters --- <\s>
example: hello<\s>
i want the output to be hello
I think i should use text parser but not sure how!
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
std::string line_;
ifstream file_("tty.txt");
if (file_.is_open())
{
while (getline(file_, line_))
{
std::cout << line_ << '\n';
}
file_.close();
}
else
std::cout << "error" << '\n';
std::cin.get();
system("PAUSE");
return 0;
}
You can load all text in one variable, and then with regex search all occurences of your desired pattern (in your case <sth>(any_aplha_numeric_character)*</sth> where * means one or more occurence, you can read about it at any std::regex tutorial)
Example:
std::smatch m;
std::string text = "<a>adsd</a> <a>esd</a>";
std::string::const_iterator searchStart(text.cbegin());
std::regex rgx("<a>[A-Za-z0-9\\s]*</a>");
while (std::regex_search(searchStart, text.cend(), m, rgx))
{
cout << m[0] << endl;
searchStart += m.position() + m.length();
}
gives: <a>adsd</a> and <a>esd</a> as a result, from which is very easy to extract that inner string

getting a sub string of a std::wstring

How can I get a substring of a std::wstring which includes some non-ASCII characters?
The following code does not output anything:
(The text is an Arabic word contains 4 characters where each character has two bytes, plus the word "Hello")
#include <iostream>
#include <string>
using namespace std;
int main()
{
wstring s = L"سلام hello";
wcout << s.substr(0,3) << endl;
wcout << s.substr(4,5) << endl;
return 0;
}
This should work: live on Coliru
#include <iostream>
#include <string>
#include <boost/regex/pending/unicode_iterator.hpp>
using namespace std;
template <typename C>
std::string to_utf8(C const& in)
{
std::string result;
auto out = std::back_inserter(result);
auto utf8out = boost::utf8_output_iterator<decltype(out)>(out);
std::copy(begin(in), end(in), utf8out);
return result;
}
int main()
{
wstring s = L"سلام hello";
auto first = s.substr(0,3);
auto second = s.substr(4,5);
cout << to_utf8(first) << endl;
cout << to_utf8(second) << endl;
}
Prints
سلا
hell
Frankly though, I think your substring calls are making weird assumptions. Let me suggest a fix for that in a minute:

How to remove first word from a string?

I'm looking for the best way to remove the first word from a std::string. This is what I have but I feel that this is overcompilicating things. What's the best and shortest way to do this? Thanks.
#include <string>
#include <iostream>
#include <sstream>
int main()
{
std::string str{"Where is everybody?"};
std::string first;
if (std::stringstream{str} >> first)
{
str.erase(str.begin(), str.begin() + first.size());
}
std::cout << str; // " is everybody?"
}
minor tweak, that leverages IO streams for the second half too :)
#include <string>
#include <iostream>
#include <sstream>
int main()
{
std::string str{"Where is everybody?"};
std::string first;
std::istringstream iss{str};
iss >> first;
std::ostringstream oss;
oss << iss.rdbuf();
std::cout << oss.str(); // " is everybody?"
}
You can do it without a stream: skip the initial spaces, locate the first space after that, walk to the next non-space, and use substr to get the rest of the string:
int i = 0;
while (isblank(str[i])) i++;
while (!isblank(str[i])) i++;
while (isblank(str[i])) i++;
str = str.substr(i);
Here is a demo on ideone.
Here is a solution using c++11's regex
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
int main ()
{
std::string s ("there is a subsequence in the string\n");
std::regex e ("(\\s*)(\\w*)(.*)");
std::cout << std::regex_replace (s,e,"$1$3");
return 0;
}
You could try using string::substr() and string::find_first_of().