测试字符串是否符合c++的正确格式
Testing if the string meets the Right format C++
我正在测试一个read in string是否匹配以下格式,不使用regex
代码的格式为:
Supplier Reference: XXXXXXX
Date & Time: XXXX
Name of Device: XXXXX
Priority: X
IP Address: XXXXXXXXXXXXXXXXXXXX
Event ID: XXXXXX
Description of Event: XXXXXXXXXXXX
我希望代码有一个cout << "Format is incorrect" << endl;
。
这是编辑,去掉了之前的尝试,回到基础来解释我的逻辑:
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;
}
我知道代码非常重复。
我希望有人能告诉我如何测试线条的顺序?
解决这个问题的一种方法是读取七行(每行数据)。如果一行为空,则丢弃它,不计算它。
然后对于每一行,在冒号':'
处将其分成两个字符串。使用左边的部分(例如"Supplier Reference"
)作为进入std::map
的键,右边的部分作为数据
然后循环遍历映射并确保每个键与文件中所需的键匹配。如果缺少一个键,或者映射中有一个未知的键(或者从文件中读取时没有足够的行),则会出现格式错误。
相关文章:
- 如何在openssl-ecc中获取十六进制格式的私钥
- 将"打开的CV图像"中的"颜色"转换为整数格式
- TDateTime格式在C++Builder中不会更改
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 检查不带转换的扫描格式
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 是否可以从格式字符串中检索"width"
- clang格式:宏的缩进
- clang格式:禁用排序包含
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 将RGB图像保存为PPM格式
- 询问在设计我的手臂模拟器功能表示格式1
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 如何将strftime中的格式错误作为异常捕获
- 将CHW格式的浮点向量转换为cv::Mat
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 如何以叮当格式设置评论的行长?
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- Vulkan 中的动态顶点缓冲区格式设置