I have a text file, here it is:
<code>a 0.240741
; 0.037037
k 0.0925926
s 0.222222
l 0.0925926
d 0.314815
ЏЌд|.–&Фsнcп—#
</code>
<code>a 0.240741
; 0.037037
k 0.0925926
s 0.222222
l 0.0925926
d 0.314815
ЏЌд|.–&Фsнcп—#
</code>
a 0.240741
; 0.037037
k 0.0925926
s 0.222222
l 0.0925926
d 0.314815
ЏЌд|.–&Фsнcп—#
I need to assign first part of text(char and their frequency) to unordered
map, and rest of text(ЏЌд|.–&Фsнcп—#) to string variable.How to do this?
I have a structure:
<code>struct data_maps {
unordered_map<char, double> char_prob;
unordered_map<char, string> char_haffcode;
string haffcode_all;
string ascii_char;
};
</code>
<code>struct data_maps {
unordered_map<char, double> char_prob;
unordered_map<char, string> char_haffcode;
string haffcode_all;
string ascii_char;
};
</code>
struct data_maps {
unordered_map<char, double> char_prob;
unordered_map<char, string> char_haffcode;
string haffcode_all;
string ascii_char;
};
Code, that supposed to parse the file and assign text to the variables
<code>data_maps parsing_file(string text)
{
data_maps data;
ifstream file(text);
char ch;
double prob;
streampos currentPosition;
if (file.is_open()) {
while (file >> ch >> prob)
{
data.char_prob[ch] = prob;
streampos currentPosition = file.tellg();
}
file.seekg(currentPosition);
string temp;
while (getline(file,temp)) {
data.ascii_char += temp;
}
}
else
cerr << "Error:file have not opened.";
return data;
}
</code>
<code>data_maps parsing_file(string text)
{
data_maps data;
ifstream file(text);
char ch;
double prob;
streampos currentPosition;
if (file.is_open()) {
while (file >> ch >> prob)
{
data.char_prob[ch] = prob;
streampos currentPosition = file.tellg();
}
file.seekg(currentPosition);
string temp;
while (getline(file,temp)) {
data.ascii_char += temp;
}
}
else
cerr << "Error:file have not opened.";
return data;
}
</code>
data_maps parsing_file(string text)
{
data_maps data;
ifstream file(text);
char ch;
double prob;
streampos currentPosition;
if (file.is_open()) {
while (file >> ch >> prob)
{
data.char_prob[ch] = prob;
streampos currentPosition = file.tellg();
}
file.seekg(currentPosition);
string temp;
while (getline(file,temp)) {
data.ascii_char += temp;
}
}
else
cerr << "Error:file have not opened.";
return data;
}
When i executing this:
<code>data_maps maps;
maps = parsing_file("D:\HC_test\testing.cmph");
for (auto pair : maps.char_prob)
{
cout << pair.first << " " << pair.second << endl;
}
cout << maps.ascii_char;
</code>
<code>data_maps maps;
maps = parsing_file("D:\HC_test\testing.cmph");
for (auto pair : maps.char_prob)
{
cout << pair.first << " " << pair.second << endl;
}
cout << maps.ascii_char;
</code>
data_maps maps;
maps = parsing_file("D:\HC_test\testing.cmph");
for (auto pair : maps.char_prob)
{
cout << pair.first << " " << pair.second << endl;
}
cout << maps.ascii_char;
i have this output:
<code>a 0.240741
; 0.037037
k 0.0925926
s 0.222222
l 0.0925926
d 0.314815
</code>
<code>a 0.240741
; 0.037037
k 0.0925926
s 0.222222
l 0.0925926
d 0.314815
</code>
a 0.240741
; 0.037037
k 0.0925926
s 0.222222
l 0.0925926
d 0.314815
New contributor
Pavlo Osip is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.