Cannot concatenate strtok's output variable. strcat and strtok

爷,独闯天下 提交于 2019-12-08 07:26:59

问题


I’ve spent hours on this program and have put several hours online searching for alternatives to my methods and have been plagued with crashes and errors all evening…

I have a few things I'd like to achieve with this code. First I’ll explain my problems, then I’ll post the code and finally I’ll explain my need for the program.

The program outputs just the single words and the concatenate function does nothing. This seems like it should be simple enough to fix...

My first problem is that I cannot seem to get the concatenate function to work, I used the generic strcat function which didn't work and neither did another version I found on the internet ( that function is used here, it is called "mystrcat" ). I want to have the program read in a string and remove "delimiters" to create a single string comprised of every word within the original string. I am trying to do with strtok and a strcat function. If there is an easier or simpler way PLEASE I’m all ears.

Another problem, which isn’t necessarily a problem but an ugly mess: the seven lines following main. I’d prefer to initialize my variables as follows: char variable[amt]; but the code I found for strtok was using a pointer and the code for the strcat function was using pointers. A better understanding of pointers && addresses for strings would probably help me out long term. However I would like to get rid of some of those lines by any means necessary. I can’t have 6 lines dedicated to only 2 variables. When I have 10 variables I do not want 30 lines up top…

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

char *mystrcat(char *output, char *firstptr);

int main() {

char str[] = "now # is the time for all # good men to come to the # aid of their     country";
char delims[] = "# ";
char resultOrig[70];    //was [20];
char firstOrig[20];
//char *result = NULL, *first = NULL;
char result = resultOrig;    //was result = &resultOrig;
char first = firstOrig;    //was   first = &firstOrig;

first = strtok( str, delims );

while( first != NULL ) {
    mystrcat(resultOrig, firstOrig);
    printf( "%s ", first );
    printf("\n %s  this should be the concat\'d string so far\n", resultOrig);
    first = strtok( NULL, delims );

}
system("pause");
return 0;
}

char *mystrcat(char *resultptr, char *firstptr)
{
char *output = resultptr;

while (*output != '\0')
    output++;
while(*firstptr != '\0')
{
    *output = *firstptr;
    output++;
    firstptr++;
}
*output = '\0';

return output;
}

This is just a test program right now but I was intending to use this for a list/database of files. My files have underscores, hyphens, periods, parentheses’, and numbers; all of which I would like to set as the “delimiters”. I was planning on going thru a loop, where I would delete a delimiter(each loop-thru change from _ to – to . etc…) and create a single string, I may want to replace the delimiters with a space or a period. And some files have spaces in them already along with the special characters I’d like to “delimit”.

I’m planning to do all this by means of scanning a text file. Within the file I also have a size in this format: “2,518,6452”. I’m hoping I can sort my database alphabetically or by size, ascending or descending. That’s just some additional information which may be useful to know for my specific questions above.

Below I have included some fictional samples of how these names could appear. my_file(2009).ext second.File-group1.extls the.third.file-vol30.lmth

I am focusing this post on: the question on how to get the concatenate function working or an alternative to strcat and/or strtok. As well as asking for help to unclutter unnecessary or redundant code.

I appreciate all the help and even all those who read through my post.

Thank you so much!


回答1:


strcat would work if you used first instead of firstOrig in your loop. No need for mystrcat. Can be simplified to:

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

int main() {
    char str[] = "now # is the time for all # good men to come to the # aid of their     country";
    char delims[] = "# ";
    char result[100] = "";  /* Original size was too small */
    char* token;
    token = strtok(str, delims);
    while(token != NULL) {
        printf("token = '%s'\n", token);
        strcat(result, token);
        token = strtok(NULL, delims);
    }
    printf("%s\n", result);
    return 0;
}

Output:

token = 'now'
token = 'is'
token = 'the'
token = 'time'
token = 'for'
token = 'all'
token = 'good'
token = 'men'
token = 'to'
token = 'come'
token = 'to'
token = 'the'
token = 'aid'
token = 'of'
token = 'their'
token = 'country'
nowisthetimeforallgoodmentocometotheaidoftheircountry



回答2:


There are several problems here:

  1. Missing intitializations fro resultOrig and firstOrig (as codaddict pointed out).

  2. first = &firstOrig doesn't do what you want from it. You later do first = strtok(str, delims), which sets first to point to somewhere in str. It doesn't read data into firstOrig.

  3. You allocate small buffers (just 20 bytes) and try to fill them with much more than this. It would overflow the stack. causing nasty bugs.




回答3:


You've not initialized the following two strings:

char resultOrig[20];
char firstOrig[20];

and you are appending characters to them. Change them to:

char resultOrig[20] = "";
char firstOrig[20] = "";

Also the name of the character array gives its starting address. So

result = &resultOrig;
first = &firstOrig;

should be:

result = resultOrig;
first = firstOrig;

Change

mystrcat(resultOrig, firstOrig);

to

mystrcat(resultOrig, first);

also make resultOrig to be large enough to hold the concatenations, like:

char resultOrig[100] = "";


来源:https://stackoverflow.com/questions/10227353/cannot-concatenate-strtoks-output-variable-strcat-and-strtok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!