I have this file dump.txt which is always 64K in size. Now I am reading in 16 bytes at a time from this file into a buffer and then doing some comparisons. I am using the read function to read the bytes.
This works fine for some time and then all of a sudden there is this error for which I am displaying 'Unexpected End of File' Below is the code. I am using VC++ 6.0.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <io.h>
#include <windows.h>
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
static int readtobuf(int fd, u8 *buf, size_t count, const char *prefix)
{
size_t bytesread=1;
size_t r2=0;
while(r2!=count && bytesread!=0)
{
bytesread=read(fd,buf+r2,count-r2);
if(bytesread==-1)
{
if(errno!=EINTR)
{
close(fd);
perror(prefix);
return -1;
}
}
else
r2+=bytesread;
}
if(r2!=count)
{
close(fd);
fprintf(stderr, "%s: Unexpected end of file\n", prefix);
printf("%s\n",buf);
printf("bytesread = %d, r2 = %d,count = %d \n",bytesread,r2,count);
return -1;
}
return 0;
}
int main(int argc, const char *argv[])
{
u8 buf[16];
int fd;
long fp = 0L;
const char *tempfile = "c:\\dump.txt";
if(((fd=open(tempfile, O_RDONLY))==-1) || _lseek(fd,fp, SEEK_SET)==-1)
{
exit(1);
}
while(fp < (65535))
{
int i;
if(readtobuf(fd, buf, 16, tempfile)==-1)
exit(1);
{
if(strncmp((char *)buf, "_SM_", 4)==0 && fp< (65535))
{
//doing some processing here. nothing with the file or buffer though
}
fp+=16;
}
if(close(fd)==-1)
{
exit(1);
}
return 0;
}
Also I also noticed whenever the application crashes using different dumps the *buf always contains these three characters (within the quotes) at the end '└ ↕'
I could have attached two sample dump.txt, but there is no option here for that.
Any insight on the above problem is very much welcome
This works fine for some time and then all of a sudden there is this error for which I am displaying 'Unexpected End of File' Below is the code. I am using VC++ 6.0.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <io.h>
#include <windows.h>
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
static int readtobuf(int fd, u8 *buf, size_t count, const char *prefix)
{
size_t bytesread=1;
size_t r2=0;
while(r2!=count && bytesread!=0)
{
bytesread=read(fd,buf+r2,count-r2);
if(bytesread==-1)
{
if(errno!=EINTR)
{
close(fd);
perror(prefix);
return -1;
}
}
else
r2+=bytesread;
}
if(r2!=count)
{
close(fd);
fprintf(stderr, "%s: Unexpected end of file\n", prefix);
printf("%s\n",buf);
printf("bytesread = %d, r2 = %d,count = %d \n",bytesread,r2,count);
return -1;
}
return 0;
}
int main(int argc, const char *argv[])
{
u8 buf[16];
int fd;
long fp = 0L;
const char *tempfile = "c:\\dump.txt";
if(((fd=open(tempfile, O_RDONLY))==-1) || _lseek(fd,fp, SEEK_SET)==-1)
{
exit(1);
}
while(fp < (65535))
{
int i;
if(readtobuf(fd, buf, 16, tempfile)==-1)
exit(1);
{
if(strncmp((char *)buf, "_SM_", 4)==0 && fp< (65535))
{
//doing some processing here. nothing with the file or buffer though
}
fp+=16;
}
if(close(fd)==-1)
{
exit(1);
}
return 0;
}
Also I also noticed whenever the application crashes using different dumps the *buf always contains these three characters (within the quotes) at the end '└ ↕'
I could have attached two sample dump.txt, but there is no option here for that.
Any insight on the above problem is very much welcome