一般遇到这种情况的人,很可能是你的数组溢出了。我们写程序应该时刻警惕,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后面出现的是一个奇怪的符号,这就是数组溢出的悲剧