[問題] while裡跑printf會把字串覆蓋掉
#include<stdio.h>
#include<string.h>
int main()
{
char input[80]={'\0'},origin[80]={'\0'},tmp[80]={'\0'};
char *token;
while(fgets(input,80,stdin))
{
strcpy(origin,input);
strtok(origin,"\n");
token=strtok(input," ");
token=strtok(NULL," \n");
if(strncmp("RSUB",token,4)==0)
strcpy(tmp," 4C0000");
printf("%s%s\n",origin,tmp);
}
return 0;
}
================================================
我用a.out<fin.txt的方式輸入
fin.txt的內容如下
================================================
1000 RSUB
1000 RSUB
1000 RSUB
1000 RSUB
================================================
我預期的結果應該是
1000 RSUB 4C0000
1000 RSUB 4C0000
1000 RSUB 4C0000
1000 RSUB 4C0000
================================================
可是跑出來的結果卻是
4C0000UB
4C0000UB
4C0000UB
4C0000UB
(每一行前面的1000 RS都被4C0000覆蓋掉了)
我用a.out<fin.txt來輸入就會出現這個問題
可是如果我用複製貼上來輸入就不會有這個問題
為什麼會這樣啊
我找不出哪裡有問題
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.119.135.80
→
06/15 09:24, , 1F
06/15 09:24, 1F
→
06/15 16:19, , 2F
06/15 16:19, 2F
→
06/15 16:29, , 3F
06/15 16:29, 3F
推
06/15 18:14, , 4F
06/15 18:14, 4F
→
06/15 18:15, , 5F
06/15 18:15, 5F
→
06/15 18:16, , 6F
06/15 18:16, 6F
→
06/15 18:16, , 7F
06/15 18:16, 7F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章