如何使用分隔符进行解析

时间:2013-04-12 06:18:55

标签: c string

我有一行格式如下:

str="WORD1\tWORD2\tWORD3\tWORD4...";

问题是我需要得到第三个字。

我一直在玩strchr和strcpy,但我只是弄得一团糟。

我唯一要做的就是保存在相同的字符串变量str中,这样我就可以继续解析直到第三列。

char *p;
p=strchr(str,'\t');
strcpy(str,str,p?);

感谢您的帮助。

谢谢!

6 个答案:

答案 0 :(得分:1)

试试这个,只是一个想法(我没有检查其他字符串):

代码不会为第一个和最后一个提供单词,只有在你需要\t的情况下才输出子字符串:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    char* str="WORD1\tWORD2\tWORD3\tWORD4";
    char *p1 = NULL;
    char *p2 = NULL;
    int i = 0, which = 3;
    char word[10]={0};

    scanf("%d", &which );
    p1 = str;
    for(i=0 ; (p1 != NULL) && (i < which-1); i++){
        p1=strchr(p1 + 1, '\t');
        if(p1 +1)
        p2=strchr(p1 + 1, '\t');
    }

    if(p1!=NULL && p2!=NULL){
        strncpy(word, p1, p2-p1);
        printf("\n %s\n", word);
    }
    return 1;
}

它的运行方式如下:

$ ./a.out 
1
:~$ gcc x.c
:~$ ./a.out 
2
    WORD2
:~$ ./a.out 
3
    WORD3
:~$ ./a.out 
4

这是你需要的吗?

答案 1 :(得分:0)

strpbrk()
strspn()
strtok()
strsep()  // define _BSD_SOURCE to have this available

可以进行解析。


您也可以通过调用regcomp()regexec()来使用正则表达式。

答案 2 :(得分:0)

<强>(1)

strchr将在字符串(man page)中找到第一个字符。

char *strchr(const char *s, int c);

<强>(2)

strcpy将source指向的字符串复制到destination(man page)指向的数组中。

char *strcpy(char *dest, const char *src);

<强>(3)

您需要使用strtok来解析输入字符串(man page)。

char *strtok(char *str, const char *delim);

查看示例:http://www.cplusplus.com/reference/cstring/strtok/

<强>(4)

您无法将字符串复制到未分配的指针中。您需要为目的地分配内存。

<强>(5)

以下示例代码向您解释:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char *str="WORD1\tWORD2\tWORD3\tWORD4...";
    char *p = NULL;
    char *out = NULL;
    int count = 0;
    int needed = 2; 

    p = strtok(str , "\t");
    while(p && count != needed) {
        p = strtok(NULL , "\t");
        count ++;
    }

    if(p) {
        out = malloc(strlen(p) + 1);
        strcpy(out, p);
        printf("String found: %s\n", out);
    }
    return 0;
}

答案 3 :(得分:0)

char *cut(int nth, char* out, const char* in){
    const char *pi=in;
    char *po=out;
    int n = 0;
    for(;;){
        *po=*pi;
        if(*po=='\t')
            *po='\0';
        if(*po == '\0'){
            ++n;
            if(n==nth)
                return out;
            else if(*pi == '\0')break;
            po=out;
        } else 
            ++po;
        ++pi;
    }
    return NULL;
}
/*
int main() {
    const char *str="WORD1\tWORD2\tWORD3\tWORD4\tWORD5";
    char word[32]={0};
    printf("%s\n", cut(3, word, str));//WORD3
    return 0;
}
*/

答案 4 :(得分:0)

很抱歉延迟,这就是我最终实现它的方式:

      int main() 
      {

          char *readbuff="WORD1\tWORD2\tWORD3\tWORD4...";
          char * pch;
          int third_col=0;
          pch = strtok (readbuff," \t");
          while ((pch != NULL)&& (TRUE == notfound))
          {
            pch = strtok (NULL, " \t");
            if (third_col==1)
            {
              dprintf (DBG_INFO,"The third word is: %s\n",pch);
              notfound=FALSE;
            }
            third_col++;
          }
    }

答案 5 :(得分:0)

简单的c ++代码:

    string s = "abc\tdef\tghi";
    stringstream ss(s);
    string a,b,c;
    ss >> a ; ss.ignore() ; ss >> b ; ss.ignore() ; ss >> c;    
    cout << a << " " << b << " " << c << endl;

输出:

abc def ghi