Search code examples
cfgets

fgets() seems to overflow input to other variables


I'm doing a read from a file, but the input seems to "overflow" into other variables.

I have these 2 variables:

char str[250];  //used to store input from stream
char *getmsg;   //already points to some other string

The problem is, when I use fgets() to read the input

printf("1TOKEN:%s\n",getmsg);
    fp=fopen("m.txt","r");
    fp1=fopen("m1.txt","w");
        if(fp!=NULL && fp1!=NULL)
printf("2TOKEN:%s\n",getmsg);
        while(fgets(str,250,fp)!=NULL){
printf("3TOKEN:%s\n",getmsg);
        printf("read:%s",str);
printf("4TOKEN:%s\n",getmsg);

I get something like this:

1TOKEN:c
2TOKEN:c
3TOKEN:b atob atobbody

read:a b atob atobbody
4TOKEN:b atob atobbody

You see how str kind of flows into getmsg. What happened there? How can I avoid this from happening?

Thanks in advance :)


in the code, "getmsg" is called "token", I thought it might have something to do with identical names or something so I changed it to getmsg, same error, so I changed it back...

                        if(buf[0]=='C'){
                            int login_error=1;

                            fp=fopen("r.txt","r");
                            if(fp!=NULL){
                                memcpy(&count,&buf[1],2);
                                pack.boxid=ntohs(count);

                                memcpy(pack.pword,&buf[3],10);
                                printf("boxid:%u pword:%s\n",pack.boxid,pack.pword);

                                while(fgets(str,250,fp)!=NULL){

/*"getmsg"===>*/                    token=strtok(str," ");
                                    token=strtok(NULL," ");//receiver uname
                                    token1=strtok(NULL," ");//pword
                                    token2=strtok(NULL," ");//boxid
                                    sscanf(token2,"%hu",&count);//convert char[] to unsigned short

                                    if(pack.boxid==count && strcmp(token1,pack.pword)==0){//uname & pword found
                                        login_error=0;
                                        printf("found:token:%s\n",token);
                                        break;
                                    }
                                }
                                if(login_error==1){
                                    count=65535;
                                    pack.boxid=htons(count);
                                }
                                if(login_error==0){
                                    count=0;
                                    pack.boxid=htons(count);
                                }
                                fclose(fp);
                            }
printf("1TOKEN:%s\n",token);
                            if(login_error==0){
                                int msg_error=1;

                                fp=fopen("m.txt","r");
                                fp1=fopen("m1.txt","w");
                                if(fp!=NULL && fp1!=NULL){
printf("2TOKEN:%s\n",token);
                                    while(fgets(str,250,fp)!=NULL){


printf("3TOKEN:%s\n",token);
                                              printf("read:%s",str);
                                        token1=strtok(str," ");//sender
                                        token2=strtok(NULL," ");//receiver
                                        token3=strtok(NULL," ");//subject
                                        token4=strtok(NULL," ");//body
                                        printf("m.txt:token1:%s token2:%s token3:%s token4:%s\n",token1,token2,token3,token4);

                                        if(msg_error==1 && strcmp(token,token2)==0){//message found
                                            msg_error=0;
                                            count=0;
                                            pack.boxid=htons(count);
                                            strcpy(pack.uname,token1);
                                            strcpy(pack.subject,token3);
                                            strcpy(pack.body,token4);
                                            printf("pack:uname:%s subject:%s body:%s token:%s token2:%s strcmp:%d\n",pack.uname,pack.subject,pack.body,token,token2,strcmp(token,token2));
                                            continue;
                                        }

                                        fprintf(fp1,"%s %s %s %s\n",token1,token2,token3,token4);
                                    }
                                    if(msg_error==1){
                                        count=65534;
                                        pack.boxid=htons(count);
                                    }
                                    printf("count:%u -> boxid:%u\n",count,pack.boxid);

                                    fclose(fp);
                                    fclose(fp1);
                                }

                                str[0]='c';
                                memcpy(&str[1],&pack.boxid,2);
                                memcpy(&str[3],pack.uname,8);
                                memcpy(&str[11],pack.subject,20);
                                memcpy(&str[31],pack.body,200);
                                str[231]='\0';

                                bytes=232;
                            }
                        }

below is m.txt, it is used to store senders, receivers, subjects and msgbodies: the naming patter is quite obvious >.^

a b atob atobbody
a c atoc atoccc
b c btoc btoccccc
b a btoa btoaaaaa

So I'm trying to get a msg stored in m.txt for the recipient "c", but it flows over, and by much coincidence, it returns the msg for "b"...


Solution

  • It looks like getmsg is pointing to the third character of your str buffer:

    `str` is "a b atob atobbody"
                ^
                |
                \__ `getmsg` is pointing there.
    

    Therefore, every time you change str by calling fgets(), the string pointed to by getmsg also changes, since it uses the same memory.