Using fgets() and strtok() to read in a file line-by-line in C?

后端 未结 4 1802
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 07:03

I\'m trying to use fgets and strtok() to read in a file line by line, and create a linked list of each different line of information.

Right now, I\'m only just putt

4条回答
  •  悲哀的现实
    2020-12-09 07:48

    strtok will modify the original string. Hence, previous pointers you stored are no longer there after each iteration.

    Simple solution is to use: strdup to allocate and copy the values.

    Just modify your assignment of value everywhere:

    result[i][0] = value;
    

    To:

    result[i][2] = strdup(value);
    

提交回复
热议问题