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

标签 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”被称为“token”,我认为可能与相同的名称或其他内容有关,所以我将其更改为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

因此,我试图获取存储在 m.txt 中的收件人“c”的消息,但它溢出了,并且很巧合的是,它返回了“b”的消息...

最佳答案

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

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

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

关于c - fgets() 似乎溢出输入到其他变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4112538/

相关文章:

c - 尝试在 C 中读取 txt 文件时出错去分段(核心转储)

c - fgets() 在 C 中不起作用?

c - EOF 上的意外 fgets 行为

c - 从 fgets() 输入中删除尾随换行符

用户输入的正确值未插入堆栈

c - 按位或和逻辑或运算符。有什么不同?

C程序编译但不执行

c - 多叉树的结构 - C

c - 读取文件时出现段错误核心转储

c - 将位图图像写入文件