今天在写程序的时候需要从一个二进制文件中读取数据,可是奇怪的是。。居然老是读不完整。。以前读文件用的都是ReadFile函数。。这次我用的是c运行时函数。。。代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(int argc,char* argv[])
{
FILE* fp;
struct stat st;
int sLen;
if(argc!=2)
{usage(argv[0]);exit(0);}
fp=fopen(argv[1],"rb");
if(fp==NULL)
{
printf("open file error!\n");
exit(0);}
fstat(fileno(fp),&st);
if(st.st_size>1024)
{
printf("shellcode is too long!\n");
exit(0);
}
printf("the size of %s is:%d\n",argv[1],st.st_size);
char* shellcode=(char*)(malloc(st.st_size));
if(!fread(shellcode,st.st_size,1,fp))
{
printf("read file error!\n");
exit(0);
}
fclose(fp);
sLen=strlen(shellcode);
printf("The source code are:\n%s\n",shellcode);
printf("The source code are:\n%s\n",shellcode);
printf("the length of source code is:%d\n",sLen);
return 0;
}
运行结果如下:
E:\lcc\lcc>csc test.bin
the size of test.bin is:57
The source code are:
U夊侅
the length of source code is:6
只读出了前6个字节,仔细想了一下估计是test.bin文件中有”\x00",因此fread函数一碰到这个就以为是到了文件的末尾。因此就不读了。。。
怎么才能把所有的内容都度出来呢。。。我一时没想出来。。还请各位明白人帮我看看。。。谢谢!!急阿。。程序都快要完工了。。拜托