c - Why does my variable change after strtok() and fgets() without modifying it? -


demonstration of problem here: http://goo.gl/71u1xa

i reading file, , in file there line:

sectie field_in #define endsec 

that indicates need store lines following 1 variable, only if line contains part "#define".

so, i'm iterating trough file , when line contains "sectie" , "field_in", retrieves word # in , stores in variable keyword.

now problem is: value in keyword changes in next loop without modifying it.

my textfile is:

sectie field_in1 #define endsec #define col1                1,1 #define col2                2,3 #define col3                5,3 ...etc 

and output of code this:

start  reading line text=sectie field_in1 #define endsec  keyword1=.. new sectie keyword2=.#define.  reading line text=#define col1              1,1  keyword1=.            1,1 . start = 1! keyword3=.            1,1 . 

my code:

int     x, status, start; char    inputline[5000 + 1]; char    copyline[5000 + 1]; char    *keyword = ""; file    *ifile;  status  = noerror; x       = 0; start   = 0;  ifile = fopen(gsz_inputfile, "r");   if(ifile == null){      status = er_open;     return status; }  while (fgets(inputline, sizeof(inputline), ifile) != null){      printf("\n\nreading line");      printf("\ntext=%s", inputline);      printf("\nkeyword1=.%s.", keyword);      strcpy(copyline, inputline);     strlwr(copyline);     if(strstr(copyline, "sectie") != null && strstr(copyline, "field_in") != null){         start = 1;          printf("\nnew sectie");          //get keyword         keyword = strtok(inputline," ");         while (keyword != null)         {             if(strstr(keyword, "#") != null){                                break;             }              keyword = strtok(null, " ");         }          printf("\nkeyword2=.%s.", keyword); //here keyword correct         continue;     }      if(start){         printf("\nstart = 1!");          printf("\nkeyword3=.%s.", keyword);          //status = storestring(inputline, keyw, x); //my actual code different         if(status != noerror){ x--; }          x++;     }        } 

i think has while (fgets(inputline, sizeof(inputline), ifile) != null) because after line executed, value changed.

why value of keyword changed after going next loop? guess has undefined behavior, can't put finger on problem.

you have inputline array. inside store string fetched fgets():

while (fgets(inputline, sizeof(inputline), ifile) != null){ 

and in loop keyword points inputline's token (which means points somewhere inside array).

//get keyword keyword = strtok(inputline," "); 

when run second fgets() array content's change, not address, keyword still points inputline content's which have changed

example:

first fgets(): inputline: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] keyword-->"" second strtok: keyword------------------------------------^  second fgets(): inputline: ['t', 'h', 'i', 's', ' ', 'c', 'h', 'a', 'n', 'g', 'e'] keyword------------------------------------^ (before strtok) 

you assumption "the value of keyword changed" false: value of keyword still same, can check printf("value of keyword: %p\n", keyword);. changed value keywordpoints to, display using %s in printf.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -