It looks like getmsg is pointing to the third character of your str buffer.
Up vote 0 down vote favorite share g+ share fb share tw.
I'm doing a read from a file, but the input seems to "overflow" into other variables. I have these 2 variables: char str250; //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 be 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(buf0=='C'){ int login_error=1; fp=fopen("r. Txt","r"); if(fp! =NULL){ memcpy(&count,&buf1,2); pack.
Boxid=ntohs(count); memcpy(pack. Pword,&buf3,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); } str0='c'; memcpy(&str1,&pack.
Boxid,2); memcpy(&str3,pack. Uname,8); memcpy(&str11,pack. Subject,20); memcpy(&str31,pack.
Body,200); str231='\0'; bytes=232; } } below is m. Txt, it is used to store senders, receivers, subjects and msgbodies: the naming patter is quite obvious >. ^ a be atob atobbody a c atoc atoccc be c btoc btoccccc be 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"... c fgets link|improve this question edited Nov 6 '10 at 16:08Yi Jiang16.7k32343 asked Nov 6 '10 at 9:12Some Noob Student601212 76% accept rate.
– Nyan Nov 6 '10 at 9:22 please post getmsg init and m. Txt – Ass3mbler Nov 6 '10 at 9:25 I think it's token=strtok(str," "); If memory serves me right, I remember strtok fills up your delimiter with '\0' in your str and passes it back to token. But I'm not too sure... – Some Noob Student Nov 6 '10 at 9:54.
It looks like getmsg is pointing to the third character of your str buffer: `str` is "a be 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.
Thank you for your insight! Ohhh... I think I see it now... strtok() seems to house the problem... – Some Noob Student Nov 6 '10 at 9:51.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.