fgets()似乎将输入溢出到其他变量

时间:2010-11-06 09:12:17

标签: c fgets

我正在读取文件,但输入似乎“溢出”到其他变量中。

我有这两个变量:

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

问题是,当我使用fgets()来读取输入

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);

我得到这样的东西:

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

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

您会看到str类型如何流入getmsg。那里发生了什么?我怎样才能避免这种情况发生?

提前致谢:)


在代码中,“getmsg”被称为“令牌”,我认为它可能与相同的名称或某事有关,所以我将其更改为getmsg,同样的错误,所以我把它改回来了......

                        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;
                            }
                        }

下面是m.txt,它用于存储发件人,接收者,主题和msgbodies: 命名模式非常明显>。^

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

所以我试图在收件人“c”中输入存储在m.txt中的msg,但它会流过,并且非常巧合,它会返回“b”的msg ...

1 个答案:

答案 0 :(得分:0)

看起来getmsg指向str缓冲区的第三个字符:

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

因此,每次通过调用str更改fgets()时,getmsg指向的字符串也会更改,因为它使用相同的内存。 / p>