open一个程序写入缓存,open创建新程序,写入,结果无法运行。
本帖最后由 ZDW86 于 2013-05-26 22:19:09 编辑
/*open一个程序写入缓存,open创建新程序,写入,结果无法运行。*/
/*代码如下,坐等高手解答*/
/*helloword 小程序而已,只有4kb多一些*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
unsigned int cal_crc(unsigned char *ptr, unsigned int len);
void main()
{
unsigned int nCal;
unsigned int nCal2;
int FD, FDNEWDATA,FDNEWDATB;
int FDsize;
int nNumber;
char buf[6] = {'0'};
char cLastOne;
char *pcEatbuf = (char*)malloc(3000);
char *pcEatbuf2;
FD = open("./helloworld",O_RDONLY);
FDsize = lseek(FD, 0, SEEK_END);
pcEatbuf = (char*)malloc(FDsize);
lseek(FD, 0, SEEK_SET);
nNumber =read(FD, pcEatbuf, 3000);
cLastOne = (char)*(pcEatbuf+FDsize-1);
pcEatbuf2 = (char*)malloc(FDsize - 3000);
read(FD, pcEatbuf, 3000);
lseek(FD, 3000, SEEK_SET);
read(FD, pcEatbuf2, FDsize - 3000);
close(FD);
//////////////////////////////////////////////////////////
nCal = cal_crc(pcEatbuf,4831);
printf("pcABCD=%d\n",nCal);
nCal = FDsize - 3000;
printf("FDsize=%d\n",FDsize);
nCal2 = cal_crc(pcEatbuf2,1831);
printf("nCal2=%d\n",nCal2);
///////////////////////////////////////////////////////////////
FDNEWDATA = open("./hellocopy2",O_WRONLY|O_CREAT,0777);
write(FDNEWDATA, pcEatbuf, 3000);
close(FDNEWDATA);
FDNEWDATB = open("./hellocopy",O_WRONLY);
lseek(FD, 3000, SEEK_SET);
write(FDNEWDATB, pcEatbuf2, FDsize - 3000);
close(FDNEWDATB);
}
unsigned int cal_crc(unsigned char *ptr, unsigned int len) {
unsigned char i;
unsigned int crc=0;
while(len--!=0) {
for(i=0x80; i!=0; i/=2) {
if((crc&0x8000)!=0) {crc*=2; crc^=0x1021;} /* 余式CRC乘以2再求CRC */
else crc*=2;
if((*ptr&i)!=0) crc^=0x1021; /* 再加上本位的CRC */
}
ptr++;
}
return(crc);
}
------解决方案--------------------大哥./helloworld这个是什么设备啊,不要说是二进制可执行文件哦!