2012-05-21 128 views
1

我實際上試圖利用這種方法旅行來提取信息出來 但是我遇到了問題,希望你們能幫我解決 我試圖提取出問題「」之間的字符串,例如「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" 
+0

*表示指針的位置 – Andres

回答

0

有一些問題與您的代碼:

  1. 在您的旅行方法,病情while (*originPtr != Destination);應該while((*originPtr!=Destination) && (*originPtr != '\0'))

  2. 對於檢索串,你的邏輯似乎有點歪斜。你可以有實際上,試圖從檢索字符串中,兩者之間的「'會後聲明中的代碼currentPtr1=Travels('"',currentPtr1);可以

    焦炭extractedstring [100] = {0};

    int i = 0; 
    
    while(((*currentPtr1 != '"') && (*currentPtr1 != '\0')) && (i<100)) 
    { 
    
        extractedstring[i] = *currentPtr1; 
    
        currentPtr1++ 
    
        i++; 
    } 
    
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); 
    } 
} 
0

首先,我建議你使用的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); 

} 

} 
相關問題