一般遇到這種情況的人,很可能是你的數組溢出了。我們寫程序應該時刻警惕,printf是遇到\0結束。
假如我們定義一個數組,char buf[24], buf="abcdefghiklmnopqrstuvwxyz"
printf("%s\n",buf); 那麼這個程序是錯誤的,因爲數組上傳是它的首地址,printf不遇到\0不會終止,它在輸出24
個字符後到棧裏隨機打印其他字符,直到遇到\0
下面我們舉個例子:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
//#include<unistd.h>
int main()
{
int fd;
char op[24];
system("rm -rf ha");
system(" ifconfig eth1 >> ha");
int n;
fd=open("ha",O_RDONLY);
n=read(fd,op,sizeof(op));
#if 1
if(fd<0){
printf("errno:%d,:%s\n",errno,strerror(errno));
return 0;
}
#endif
printf("%d\n",n);
printf("op的值爲:%s\n",op);
close(fd);
}
Eth後面出現的是一個奇怪的符號,這就是數組溢出的悲劇