【问题标题】:Extracting a string in between symbols提取符号之间的字符串
【发布时间】:2012-05-21 07:33:52
【问题描述】:

我其实是想利用 Travels 这个方法来提取信息 但是我做起来有问题,我希望你们能帮助我 我在尝试提取“”之间的字符串时遇到问题,例如“hello”来提取你好。以下是我的方法。

旅行方法

char *Travels(char Destination, char *originPtr)
{
do
{
    originPtr++;
}while (*originPtr != Destination);

originPtr++;
return originPtr;
}

在我的主要

int main()
{
//pointer for reading of file
char *startPtr1;
char Lines[256];

//read file and perform 
ifstream chordfile("myfile.txt");
if (chordfile.is_open())
{
    do
    {
        chordfile.getline(Lines, 256);
        startPtr1 = Lines;
        readFileInput(startPtr1);

    }while(chordfile.eof() == false);
    chordfile.close();
}

return 0;
}

在我的 readFileInput 方法中(我将展示部分方法)

//if it is insert.
if (strcmp(Stringg, "insert") == 0)
{
    char *SpecialPtr1;
    currentPtr1=Travels(' ',startPtr1);  // travels to Insert(*) 7 "your_data"
    int insertPeerNum = (int)atoi(currentPtr1); // travels to Insert (7) "your_data"
    currentPtr1=Travels(' ',currentPtr1); // travels to Insert 7(*)"your_data"
    currentPtr1=Travels('"',currentPtr1);
    SpecialPtr1=Travels('"',currentPtr1);
    *******this is the area which I am actually stucked at**********
    }

在文本文件中

Insert 7 "your_data"
Insert 7 "hello"

【问题讨论】:

  • *表示指针的位置

标签: c++ string file pointers text-extraction


【解决方案1】:

由于您没有指定homework 标签,我建议您使用std::string。这种数据结构带有许多搜索和子字符串组合。

例如在双引号之间查找文本:

#include <string>

//...

std::string::size_type    start_position = 0;
std::string::size_type    end_position = 0;
std::string               text = "My \"cat\" is black.\n";
std::string               found_text;

start_position = text.find("\"");
if (start_position != std::string::npos)
{
  ++start_position; // start after the double quotes.
  // look for end position;
  end_position = text.find("\"");
  if (end_position != std::string::npos)
  {
     found_text = text.substr(start_position, end_position - start_position);
  }
}

【讨论】:

    【解决方案2】:

    您的代码存在一些问题:

    1. 在您的 Travels 方法中,条件 while (*originPtr != Destination); 应为 while((*originPtr!=Destination) &amp;&amp; (*originPtr != '\0'))

    2. 关于检索字符串,您的逻辑似乎有点歪曲。实际上,您可以尝试从两个 '"' 之间检索字符串。语句 currentPtr1=Travels('"',currentPtr1); 之后的代码可以是

      char 提取字符串[100] = {0};

      int i = 0;
      
      while(((*currentPtr1 != '"') && (*currentPtr1 != '\0')) && (i<100))
      {
      
         extractedstring[i] = *currentPtr1;
      
         currentPtr1++
      
         i++;
      }
      

    【讨论】:

      【解决方案3】:

      首先,我建议您使用 std::string 处理方法,这将有很大帮助:) 参考:string

      // Helper Function 
      
      inline bool find_first(size_t& pos, const std::string& st, const char flag='\"')
         {
             if(pos!= std::string::npos)
                  pos=st.find_first_of(flag, pos);
             return (pos!= std::string::npos);
         }
      
      inline bool find_last(size_t& pos, const std::string& st, const char flag='\"')
         {
            if(pos!= std::string::npos)
               pos=st.find_last_of(flag);
            return (pos!= std::string::npos);
         }
       template <class T>
         inline T from_string(const std::string& s)
         {
             T t;
             std::stringstream ss(s);
             ss >> t;
             return t;
         }
      
      
      
      // Excerp Sample Code
      
      std::string stdline = Lines.
      
      
      size_t start
      size_t end;
      std::string text;
      std::string numberText;
      int number;
      
      
      if (stdLine.compare("insert"))
      {
      
      
      if (find_first(start, stdLine,' ') && find_lats(end,stdLine,' ')
      {
      
          numberText=stdLine.substr(start+1,end);
          number = from_string(numberText);
      }
      
      if (find_first(start, stdLine) && find_lats(end,stdLind)
      {
          text=stdLine.substr(start+1,end); 
      
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-05
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        • 2021-07-18
        相关资源
        最近更新 更多