English 中文(简体)
在符号间抽取字符串
原标题:Extracting a string in between symbols

I am actually trying to make use of this method Travels to extract information out However I have problems doing it, I hope you guys will be able to help me out I have problems trying to extract a string in between of " ", for eg "hello" to extract the hello. These are my methods below.

旅行方法

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;
}

在我的读法中( 我将显示部分方法)

//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"
问题回答

由于您没有指定 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.
";
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);
  }
}

您的代码存在一些问题 :

  1. 在您的旅行方法中,条件 (* textPtr!= 目的地); 应该是 ((* textPtr!=目的地) & amp; & amp; (* textPtr!= ) (* textPtr!= ) 。

  2. 关于检索字符串,您的逻辑似乎有点偏斜。 实际上, 您本可以尝试从两者之间取回字符串 " 。 语句 ptr1=Travelels ( ", ptr1; 之后的代码可以是: ", ptr1; ", ptr1; " /code > 。

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

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

First, I recommend you to use std::string process methods, this will help a lot :) Reference: 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); 

}

}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签