测试字符串是否符合c++的正确格式

Testing if the string meets the Right format C++

本文关键字:格式 c++ 字符串 是否 测试      更新时间:2023-10-16

我正在测试一个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的键,右边的部分作为数据

然后循环遍历映射并确保每个键与文件中所需的键匹配。如果缺少一个键,或者映射中有一个未知的键(或者从文件中读取时没有足够的行),则会出现格式错误。