c - Strange behavior after fseek() has worked -


i've came across strange behavior. while debugging, when while-loop loops @ first time: after going though /* "data-url" */ , /* "data-author" */ parts of code have next result in debugging windows -> watches:

(i'm using code::blocks ide, ubuntu 13.04)

the length of dataurl_tempstring 8 bytes, length of dataauthor_tempstring 11 bytes, length of dataname_tempstring 9 bytes...

1st pic

but after going through /* data-name */ part of code have the result confuses me:

2nd pic

now not of size 8, 11 , 9 bytes!

what matter?

could me finding reason of such behavior?


here code of function:

int substring_search(char *fnamenew, char *strurl, char *strauthor, char *strname) {      file *fp;     file *ofp_erase;     file *ofp;     char ch_buf;     int count = 0;      char dataurl[8] = "";     char dataauthor[11] = "";     char dataname[9] = "";     char *dataurl_tempstring = &dataurl[0];     char *dataauthor_tempstring = &dataauthor[0];     char *dataname_tempstring = &dataname[0];       if( (fp = fopen("output_temp.txt", "r")) == null) {         printf("file not opened.\n");         return (-1);     }     else {         /* erasing 'new' file if exists */         ofp_erase = fopen(fnamenew, "w");         fclose(ofp_erase);     }        ofp = fopen(fnamenew, "a");     rewind(fp);      while(!feof(fp)) {          /* "data-url" */         fread(dataurl_tempstring, 8, sizeof(char), fp);         if(memcmp(dataurl_tempstring, strurl) == 0) {             fseek(fp, 2, seek_cur);     // going required place copy string             while( (ch_buf = getc(fp)) != '"') {                 fputc(ch_buf, ofp);             }             fputc('\n', ofp);         }         fseek(fp, -8, seek_cur);           /* "data-author" */         fread(dataauthor_tempstring, 11, sizeof(char), fp);         if(memcmp(dataauthor_tempstring, strauthor) == 0) {             fseek(fp, 2, seek_cur);     // going required place copy string             while( (ch_buf = getc(fp)) != '"') {                 fputc(ch_buf, ofp);             }             fputc(' ', ofp);             fputc('-', ofp);             fputc(' ', ofp);         }         fseek(fp, -11, seek_cur);           /* "data-name" */         fread(dataname_tempstring, 9, sizeof(char), fp);         if(memcmp(dataname_tempstring, strname) == 0) {             fseek(fp, 2, seek_cur);     // going required place copy string             while( (ch_buf = getc(fp)) != '"') {                 fputc(ch_buf, ofp);             }             //fputc() not needed         }         fseek(fp, -8, seek_cur); // jumping on 1 symbol beginning: `-8` instead of `-9`...           count++;         if(count == 5)             break;     }      rewind(fp);     fclose(fp);     fclose(ofp);      return 0; } 

you might change call

int strcmp(const char *s1, const char *s2); 

to become calls

int memcmp(const void *s1, const void *s2, size_t n); 

this shall fix issue, long not use other members of str*() family of function on (non 0-terminated) char arrays.

note: memcmp() always compares number of characters passed 3rd parameter (n). might not want.


update:

alternativly (as mixure of both calls above) there is:

int strncmp(const char *s1, const char *s2, size_t n); 

which compares until finds 0-terminator in either s1 or s2 , maximum of n characters.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -