Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3335110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:58:50+00:00 2026-05-17T23:58:50+00:00

I’m doing a read from a file, but the input seems to overflow into

  • 0

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”…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:58:50+00:00Added an answer on May 17, 2026 at 11:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.