1

私は実際にこのメソッドを利用しようとしています。Travelsを使用して情報を抽出していますが、問題があります。「」の間にある文字列を抽出しようとして問題が発生した場合は、皆さんが私を助けてくれることを願っています。 hello」を使用して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"
4

3 に答える 3

2

タグを指定していないので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);
  }
}
于 2012-05-21T13:35:46.407 に答える
0

まず、std::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); 

}

}
于 2012-05-21T13:48:08.937 に答える
0

あなたのコードにはいくつかの問題があります:

  1. Travels メソッドでは、条件 while (*originPtr != Destination);は次のようになります。while((*originPtr!=Destination) && (*originPtr != '\0'))

  2. 文字列の取得に関しては、ロジックが少し歪んでいるように見えます。実際には、2 つの「"」の間から文字列を取得しようとした可能性があります。ステートメントの後のコードは次のcurrentPtr1=Travels('"',currentPtr1); ようになります。

    char 抽出文字列[100] = {0};

    int i = 0;
    
    while(((*currentPtr1 != '"') && (*currentPtr1 != '\0')) && (i<100))
    {
    
       extractedstring[i] = *currentPtr1;
    
       currentPtr1++
    
       i++;
    }
    
于 2012-05-21T13:03:56.640 に答える