我是一个初学者,我无法理解如何使字符串向量中的内容成为char并仍然位于同一元素中。
我正在获取一个文件,其中同一行中有多条消息。因此,该程序将在任何有定界符“ 3 = Value”的地方创建一个新行,并将该行存储到字符串向量中。每行将获得一个校验和值,该值必须考虑该行的每个字符。
我正在努力查看如何使字符串向量中的每个元素成为char,以便它可以遍历行/元素中的每个字符以生成校验和。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
std::vector<std::string> SortString(std::string str, std::string delimiter)
{
std::size_t pos_previous = 0; // start of line
std::size_t pos_current; // end of line
std::size_t delimiter_length = delimiter.length();
std::string line_number;
std::string full_message;
std::string checksum_value = "checksum=";
std::vector<std::string> vstr;
while( (pos_current = str.find(delimiter, pos_previous)) != std::string::npos)
{ // looks for delimiter from previous position
line_number = str.substr(pos_previous, pos_current - pos_previous);
pos_previous = pos_current + delimiter_length; // start of line is now the end of the previous line.
full_message = delimiter + line_number;
std::size_t pos_checksum = 0;
while( ( pos_checksum = full_message.find(checksum_value)) != std::string::npos )
full_message.erase(static_cast<int>(pos_checksum), 6);
vstr.push_back(full_message);
}
return vstr;
}
void GenerateCheckSum( char *b, long bLen )
{
// Takes each character and a method is used to give a unique number to a char and generate a value.
}
int main()
{
std::ifstream file("filename.extension");
// stuff here
while(file.is_open())
{
std::string delim = "3=VALUE";
std::string message;
std::getline(file, message);
std::vector<std::string> v = SortString(message, delim);
for(int i = 0; i < v.size(); ++i)
std::cout << v.at(i) << "\n";
file.close();
}
return 0;
}
感谢您抽出宝贵的时间来提供帮助。使用旧版的cpp