2009-06-30 96 views
1

我將嘗試解釋問題。解析第一個和最後一個單詞的標記字符串(C)

我得到一個包含註冊表項的字符串。例如:

HKEY_CURRENT_USER\Software\MyProgram\SomeOtherValue\SomeKey 

現在,我需要將該字符串解析爲3個不同的char(或char *)變量。解析後它會是這樣的:

string1 = HKEY_CURRENT_USER 
string2 = \Software\MyProgram\SomeOtherValue\ /* with the '\' */ 
string3 = SomeKey 

不僅我需要組反斜槓;我也不知道他們中有多少人。我能有這樣的事情:

HKEY_CURRENT_USER\Software\SomeKey 

或類似的東西:

HKEY_CURRENT_USER\Software\SomeValue\SomeOthervalue\Someblah\SomeKey 

strtok()嘗試和strcspn(),但我發現很困惑在這裏... 任何想法如何完成這件事? 代碼表示讚賞。

謝謝!

+0

@Carl:感謝您編輯錯別字。我匆忙地打字。 – wonderer 2009-06-30 18:56:30

回答

2

僞代碼:

步驟1:快進,直到第一個 「\」,注意指數。

步驟2:從尾部向後掃描到最後一個「\」 (倒退時遇到的第一個「\」),請注意索引。

第3步:將相關的部分打包成3個字符串。

代碼:(不依賴於strrchr,或其他方法,你似乎有問題與)

void ParseRegEntry(char* regKey, char** TopLevel, char** Path, char** Key); 

int main(void) 
{ 
    char* regKey = "HKEY_CURRENT_USER\\Software\\MyProgram\\SomeOtherValue\\SomeKey"; 
    char* TopLevel; 
    char* Path; 
    char* Key; 

    ParseRegEntry(regKey, &TopLevel, &Path, &Key); 

    printf("1: %s\n2: %s\n3: %s\n", TopLevel, Path, Key); 
    free(TopLevel); 
    free(Path); 
    free(Key); 

    return 0; 
} 

void ParseRegEntry(char* regKey, char** TopLevel, char** Path, char** Key) 
{ 
    int firstDelimiter = 0; 
    int lastDelimiter = strlen(regKey)-1; 
    int keyLen; 

    while(regKey[firstDelimiter] != '\\') 
    { 
     firstDelimiter++; 
    } 

    while(regKey[lastDelimiter] != '\\') 
    { 
     lastDelimiter--; 
    } 
    keyLen = strlen(regKey) - lastDelimiter-1; 

    *TopLevel = (char*)malloc(firstDelimiter+1); 
    strncpy(*TopLevel, regKey, firstDelimiter); 
    (*TopLevel)[firstDelimiter] = '\0'; 

    *Path = (char*)malloc(lastDelimiter - firstDelimiter+2); 
    strncpy(*Path, regKey+firstDelimiter, lastDelimiter - firstDelimiter); 
    (*Path)[lastDelimiter-firstDelimiter] = '\0'; 


    *Key = (char*)malloc(keyLen+1); 
    strncpy(*Key, regKey+lastDelimiter+1, keyLen); 
    (*Key)[keyLen] = '\0'; 
} 
+0

謝謝。工作非常順利。 – wonderer 2009-06-30 19:21:48

+0

請注意,在急於獲取代碼時,我沒有完全測試邊界。我建議你重新檢查一下我的數學(+1在這裏...... -1在那裏...... +2在另一個地方)。驗證您的輸入,並準備好regKey是錯誤輸入,malloc失敗或其他錯誤發生的可能性。 – abelenky 2009-06-30 19:24:31

2

將該字符串複製到一個已分配的字符串中,並將想要截斷該字符串的變量置於'\ 0'中。

您可以使用strchr函數「掃描」斜線字符串。

void to_split(char *original, int first_slash, int second_slash, char **first, char **second, char **third) { 
     int i; 
     char *first_null; 
     char *second_null; 
     char *allocated; 

     if (first_slash >= second_slash) 
       return; 
     allocated = malloc(strlen(original) + 1); 
     *first = allocated; 
     strcpy(allocated, original); 
     for (i = 0, first_null = allocated; i < first_slash && (first_null = strchr(first_null,'\\')); i++); 

     if (first_null) { 
       *first_null = '\0'; 
       *second = first_null + 1; 
     } 

     second_null = allocated + strlen(original); 
     i = 0; 
     while (i < second_slash && second_null > allocated) 
      i += *second_null-- == '\\'; 

     if (++second_null > allocated) { 
      *second_null = '\0'; 
      *third = second_null + 1; 
     } 
} 

用法:

int main (int argc, char **argv) { 
     char *toSplit = "HKEY_CURRENT_USER\\Software\\MyProgram\\SomeOtherValue\\SomeKey"; 
     char *first; 
     char *second; 
     char *third; 
     to_split(toSplit, 1, 3, &first, &second, &third); 
     printf("%s %s %s\n", first, second, third); 
     return 0; 
} 

它不是世界上最好的代碼,但它給你的想法。

+0

我試過使用strtok()和strcspn(),但我一直迷路。你有一個例子嗎? 謝謝! – wonderer 2009-06-30 18:52:44

+0

等一下,我會給你的代碼;) – akappa 2009-06-30 18:56:44

2

,和strchr(字符*,字符):定位串字符中第一次出現

strrchr(字符*,字符):定位串字符的最後一次出現

char* str = "HKEY_CURRENT_USER\Software\MyProgram\SomeOtherValue\SomeKey"; 
char token1[SIZE], token2[SIZE], token3[SIZE]; 

char* first = strchr(str, '\\'); 
char* last = strrchr(str, '\\')+1; 

strncpy(token1, str, first-str); 
token1[first-str] = '\0'; 
strncpy(token2, first, last-first); 
token2[last-first] = '\0'; 
strcpy(token3, last); 

我們使用,和strchr找到第一個 '\',和strrchr到f ind最後的'\'。然後根據這些位置複製到token1,token2,token3。

我決定只使用固定大小的緩衝區而不是calloc-ing,因爲這並不重要。我一直在搞亂它。:)

1

下面是使用strchr和strrchr在字符串中向前和向後掃描'\'的示例。

char str[] = "HKEY_CURRENT_USER\Software\MyProgram\SomeOtherValue\SomeKey"; 
char *p, *start; 
char root[128], path[128], key[128]; 

p = strchr (str, '\\'); 
strncpy (root, str, p - str); 
start = p; 
p = strrchr (str, '\\') + 1; 
strncpy (path, start, p - start); 
strcpy (key, p); 
相關問題