Issue when using pointer to line tokens in C

十年热恋 提交于 2020-03-25 12:34:04

问题


I have created a program that requires reading a CSV file that contains bank accounts and transaction history. To access certain information, I have a function getfield which reads each line token by token:

const char* getfield(char* line, int num)
{
    const char *tok;
    for (tok = strtok(line, ",");
            tok && *tok;
            tok = strtok(NULL, ",\n"))
    {
        if (!--num)
            return tok;
    }
    return NULL;
}

I use this later on in my code to access the account number (at position 2) and the transaction amount(position 4):

...
while (fgets(line, 1024, fp))
{


        char* tmp = strdup(line); 

        //check if account number already exists

        char *acc = (char*) getfield(tmp, 2); 
        char *txAmount = (char*)getfield(tmp, 4);

        printf("%s\n", txAmount);
        //int n =1;
        if (acc!=NULL && atoi(acc)== accNum && txAmount !=NULL){
                if(n<fileSize)
                {
                        total[n]= (total[n-1]+atof(txAmount));
                        printf("%f", total[n]);
                        n++;

                }

         }
         free(tmp1); free(tmp2);
}
...

No issue seems to arise with char *acc = (char*) getfield(tmp, 2), but when I use getfield for char *txAmount = (char*)getfield(tmp, 4) the print statement that follows shows me that I always have NULL. For context, the file currently reads as (first line is empty):


AC,1024,John Doe
TX,1024,2020-02-12,334.519989
TX,1024,2020-02-12,334.519989
TX,1024,2020-02-12,334.519989

I had previously asked if it was required to use free(acc) in a separate part of my code (Free() pointer error while casting from const char*) and the answer seemed to be no, but I'm hoping this question gives better context. Is this a problem with not freeing up txAmount? Any help is greatly appreciated !

(Also, if anyone has a better suggestion for the title, please let me know how I could have better worded it, I'm pretty new to stack overflow)


回答1:


Your getfield function modifies its input. So when you call getfield on tmp again, you aren't calling it on the right string.

For convenience, you may want to make a getfield function that doesn't modify its input. It will be inefficient, but I don't think performance or efficiency are particularly important to your code. The getfield function would call strdup on its input, extract the string to return, call strdup on that, free the duplicate of the original input, and then return the pointer to the duplicate of the found field. The caller would have to free the returned pointer.




回答2:


The issue is that strtok replaces the found delimiters with '\0'. You'll need to get a fresh copy of the line.
Or continue where you left off, using getfield (NULL, 2).



来源:https://stackoverflow.com/questions/60686644/issue-when-using-pointer-to-line-tokens-in-c

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