3

I'm having a lot of trouble figuring this out. I have a C string, and I want to remove the first part of it. Let's say its: "Food,Amount,Calories". I want to copy out each one of those values, but not the commas. I find the comma, and return the position of the comma to my method. Then I use

strncpy(aLine.field[i], theLine, end);

To copy "theLine" to my array at position "i", with only the first "end" characters (for the first time, "end" would be 4, because that is where the first comma is). But then, because it's in a Loop, I want to remove "Food," from the array, and do the process over again. However, I cannot see how I can remove the first part (or move the array pointer forward?) and keep the rest of it. Any help would be useful!

Ethan Mick
  • 9,517
  • 14
  • 58
  • 74

5 Answers5

3

What you need is to chop off strings with comma as your delimiter.

You need strtok to do this. Here's an example code for you:

int main (int argc, const char * argv[]) {


    char *s = "asdf,1234,qwer";
    char str[15];
    strcpy(str, s);
    printf("\nstr: %s", str);
    char *tok = strtok(str, ",");
    printf("\ntok: %s", tok);
    tok = strtok(NULL, ",");
    printf("\ntok: %s", tok);
    tok = strtok(NULL, ",");
    printf("\ntok: %s", tok);

    return 0;
}

This will give you the following output:

str: asdf,1234,qwer
tok: asdf
tok: 1234
tok: qwer
Neilvert Noval
  • 1,655
  • 2
  • 15
  • 21
  • 1
    Actually, on most machines, this will crash when strtok tries to write to the read-only string constant. – Chris Dodd Dec 13 '10 at 05:24
  • yup. That code is for demonstration purposes only with regards to usage of strtok. :-) – Neilvert Noval Dec 13 '10 at 05:28
  • @ChrisDodd: hastily upvoted your comment (no undo) but don't agree. Constant string `*s` is copied to cstring `str`, every call to strtok modifies cstring (replacing `,` with '0' terminator) and returns the address of the token. So there is no constant read-only string rewritten there. Am I wrong? – Salvador Apr 19 '14 at 23:07
  • @Salvador: You are not wrong -- the comment applies to the original code before it was edited (in response) to add the copy... – Chris Dodd Apr 21 '14 at 16:25
2

If you have to keep the original string, then strtok. If not, you can replace each separator with '\0', and use the obtained strings directly:

char s_RO[] = "abc,123,xxxx", *s = s_RO;
while (s){
    char* old_str = s;
    s = strchr(s, ',');
    if (s){
        *s = '\0';
        s++;
    };
    printf("found string %s\n", old_str);
};
ruslik
  • 14,714
  • 1
  • 39
  • 40
0

The function you might want to use is strtok()

Here is a nice example - http://www.cplusplus.com/reference/clibrary/cstring/strtok/

harithski
  • 666
  • 1
  • 6
  • 18
0

Personally, I would use strtok().

I would not recommend removing extracted tokens from the string. Removing part of a string requires copying the remaining characters, which is not very efficient.

Instead, you should keep track of your positions and just copy the sections you want to the new string.

But, again, I would use strtok().

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

if you know where the comma is, you can just keep reading the string from that point on.

for example

void readTheString(const char *theLine)
{
    const char *wordStart = theLine;
    const char *wordEnd = theLine;

    int i = 0;
    while (*wordStart) // while we haven't reached the null termination character
    {
        while (*wordEnd != ',')
            wordEnd++;
        // ... copy the substring ranging from wordStart to wordEnd
        wordStart = ++wordEnd; // start the next word
    }
}

or something like that.
the null termination check is probably wrong, unless the string also ends with a ','... but you get the idea.

anyway, using strtok would probably be a better idea.

filipe
  • 3,370
  • 1
  • 16
  • 22