开发者

strtok - how avoid new line to and put to array of strings?

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, ",");

     }
}

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];

Thanks for help.


edit:

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)"
开发者_StackOverflow

edit2 [18:16]

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. :-)


Give this a shot:

#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, ",");       
  } 
}

And then, in main:

#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);
  ...
}

Some explanations.

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]);

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.

When we call split:

split(input, strs);

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.


If I understand correctly, you want to save the strings obtained by strtok. In that case you'll want to declare splitstr as char[MAX_LINES][32] and use strcpy, something like this:

strcpy(splitstr[i], p);

where i is the ith string read using strtok.


Note that I'm no ansi C expert, more C++ expert, but here's working code. It uses a fixed size array, which may or may not be an issue. To do anything else would require more complicated memory management:

/* Not sure about the declaration of splitstr here, and whether there's a better way.
   char** isn't sufficient. */
int split(char* str, char splitstr[256][32])
{
   char* p;
   int i=0;
   p = strtok(str,",");
   while(p) {
   strcpy(splitstr[i++], p);
       p = strtok (NULL, ",");
   }
   return i;
}

int main(int argc, char* argv[])
{
 char input[256];
  char result[256][32];
  strcpy(input, "aa,bbb,ccc,ddd");
  int count = split(input, result);

  for (int i=0; i<count; i++) {
    printf("%s\n", result[i]);
  }
      printf("the end\n");
}

Note that I supply "aa,bbb,ccc,ddd" in and I get {"aa", "bbb", "ccc", "ddd" } out. No newlines on the result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜