C++ 测试字符串是否符合正确的格式C++;
我目前正在尝试在不使用正则表达式的情况下测试读入字符串是否与以下格式匹配 代码应采用的格式为:C++ 测试字符串是否符合正确的格式C++;,c++,C++,我目前正在尝试在不使用正则表达式的情况下测试读入字符串是否与以下格式匹配 代码应采用的格式为: Supplier Reference: XXXXXXX Date & Time: XXXX Name of Device: XXXXX Priority: X IP Address: XXXXXXXXXXXXXXXXXXXX Event ID: XXXXXX Description of Event: XXXXXXXXXXXX 我希望代码有一个cout解决这个问题的一个方法是读取
Supplier Reference: XXXXXXX
Date & Time: XXXX
Name of Device: XXXXX
Priority: X
IP Address: XXXXXXXXXXXXXXXXXXXX
Event ID: XXXXXX
Description of Event: XXXXXXXXXXXX
我希望代码有一个
cout解决这个问题的一个方法是读取七行(每行数据一行)。如果一行是空的,则丢弃它,不计算它
然后,对于每一行,在冒号处将其拆分为两个字符串:“
”。将左侧部分(如“供应商参考”)用作a的键,将右侧部分用作数据
然后在映射上循环,确保每个键与文件中所需的键匹配。如果缺少密钥,或者映射中存在未知密钥(或者读取文件时没有足够的行),则会出现格式错误。请提供可编译代码。。
using namespace std;
int f;
int main()
{
string mystring;
ifstream myfile ("test.txt");
if (myfile.is_open())
{ while (getline (myfile,mystring))
{
//searches the text entered//
string search;
size_t pos;
{
//searches the text for the Date entry//
{
search = "Supplier Reference:";
pos = mystring.find(search);
{
if (pos != string::npos)
{
cout<<mystring<<endl;
f=f-1;
}
else
{
++f;
}
}
}
{
search = "Date & Time:";
pos = mystring.find(search);
{
if (pos != string::npos)
{
cout<<mystring<<endl;
f = f - 1;
}
else
{
++f;
}
}
}
{
search = "Name of Device:";
pos = mystring.find(search);
{
if (pos != string::npos)
{
cout<<mystring<<endl;
f = f - 1;
}
else
{
++f;
}
}
}
{
search = "Priority:";
pos = mystring.find(search);
{
if (pos != string::npos)
{
cout<<mystring<<endl;
f = f - 1;
}
else
{
++f;
}
}
}
{
search = "IP Address";
pos = mystring.find(search);
{
if (pos != string::npos)
{
cout<<mystring<<endl;
f = f - 1;
}
else
{
++f;
}
}
}
{
search = "Event ID:";
pos = mystring.find(search);
{
if (pos != string::npos)
{
cout<<mystring<<endl;
f = f - 1;
}
else
{
++f;
}
}
}
{
search = "Description of Event:";
pos = mystring.find(search);
{
if (pos != string::npos)
{
cout<<mystring<<endl;
f = f - 1;
}
else
{
++f;
}
}
}
}
}
{
if (f>35)
cout << f << "Field is missing, Ticket is formatted incorrectly" << endl;
}
}
system ("pause");
return 0;
}