strtok的 - 如何避免新的生产线并投入到字符串数组? [英] strtok - how avoid new line to and put to array of strings?

查看:107
本文介绍了strtok的 - 如何避免新的生产线并投入到字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我欺骗我的话题真的很抱歉,我搜索它没有结果在这里。
我有code

if i dupe topic i really sorry, i searched for it with no result here. I have code

void split(char* str, char* splitstr)
{
     char* p;
     char splitbuf[32];
     int i=0;
     p = strtok(str,",");
     while(p!= NULL)
     {
               printf("%s", p);    
               sprintf(&splitstr[i],"%s",p);
               i++;
               p = strtok (NULL, ",");

     }
}

我如何使用适当的sprintf通过的strtok把splited话字符串数组?
我能以某种方式避免的strtok创建断裂线?
我在ANSI C的编程我声明数组splitstr和STR以同样的方式。

How can i use proper sprintf to put the splited words by strtok to string array? Can i somehow avoid breaklines created by strtok? I am programming in ANSI C. I declared array splitstr and str the same way.

char* splitstr;//in main char splitstr[32];

感谢您的帮助。

编辑:


i would like do something like this:
INPUT (it is a string) > "aa,bbb,ccc,ddd"
I declare: char tab[33];
OUTPUT (save all items to array of strings if it is even possible) > 
tab[0] is "aa"
tab[1] is "bbb"
...
tab[3] is "ddd" but not "ddd(newline)"


EDIT2 [18:16]


edit2 [18:16]

我忘了补充一点,数据串是从文件的阅览行。这就是为什么我写了一篇关于DDD(新行)。我之后新的生产线也被strtok的但作为其他项目列出找到。顺便说一下所有的答案都很好地去思考这个问题。几秒钟前我的笔记本电脑已经打破了(我不知道为什么屏幕消失黑色),只要我接过我的电脑控制,我会检查codeS。 : - )

I forgot add that the data string is from reading line of file. That's why i wrote about "ddd(newline)". I found after that the new line was also shown by strtok but as another item. By the way all answers are good to think over the problem. Few seconds ago my laptop has Broken (i dont know why the screen gone black) As soon as i take control over my pc i will check codes. :-)

推荐答案

给这一个镜头:

#include <stdlib.h>
#include <string.h>
...
void split(char *str, char **splitstr) 
{      
  char *p;      
  int i=0;      

  p = strtok(str,",");      
  while(p!= NULL)      
  {                
    printf("%s", p);
    splitsr[i] = malloc(strlen(p) + 1);
    if (splitstr[i])
      strcpy(splitstr[i], p);
    i++;
    p = strtok (NULL, ",");       
  } 
}

,然后在

#define MAX_LEN  ... // max allowed length of input string, including 0 terminator
#define MAX_STR  ... // max allowed number of substrings in input string

int main(void)
{
  char input[MAX_LEN]; 
  char *strs[MAX_STR] = {NULL}; 
  ...
  split(input, strs);
  ...
}

一些解释。

可疑交易报告定义主为指针数组字符。每个数组元素将指向从输入字符串提取的字符串。在主,所有的分配是一个指针数组,每个元素初始值为null;每个元素的内存将在拆分中被分配使用 malloc函数的基础上,串的长度。的某处的你与可疑交易报告完成后,您需要使用到释放这些指针免费

strs is defined in main as an array of pointer to char. Each array element will point to a string extracted from the input string. In main, all that's allocated is the array of pointers, with each element initially NULL; the memory for each element will be allocated within the split function using malloc, based on the length of the substring. Somewhere after you are finished with strs you will need to deallocate those pointers using free:

for (i = 0; i < MAX_STR; i++)
  free(strs[i]);

现在,为什么 splitstr 声明为的char ** 而不是的char * [ MAX_STR] ?除非它是的sizeof &放大器的操作; 运营商,或者是文字被用来初始化另一个字符串数组在声明中,数组前pression将其类型隐式地从转换牛逼 N个元素的数组来指针至T ,和前pression的价值将是数组中的第一个元素的位置。

Now, why is splitstr declared as char ** instead of char *[MAX_STR]? Except when it is the operand of the sizeof or & operators, or is a string literal being used to initialize another array in a declaration, an array expression will have its type implicitly converted from N-element array of T to pointer to T, and the value of the expression will be the location of the first element in the array.

当我们调用拆分

split(input, strs);

数组前pression 输入被隐式地键入的char [MAX_LENGTH] 转换为的char * (T == 字符),以及阵列前pression 可疑交易报告被隐式类型从的char * [MAX_STRS] 的char ** (T == <$转换C $ C>的char * )。因此, splitstr 收到指针值的两个参数,而不是数组值。

the array expression input is implicitly converted from type char [MAX_LENGTH] to char * (T == char), and the array expression strs is implicitly converted from type char *[MAX_STRS] to char ** (T == char *). So splitstr receives pointer values for the two parameters, as opposed to array values.

这篇关于strtok的 - 如何避免新的生产线并投入到字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆