Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unexpected end of file when reading 1

Status
Not open for further replies.

bkowlagi

Programmer
Apr 8, 2003
28
US
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, &quot;%s: Unexpected end of file\n&quot;, prefix);
printf(&quot;%s\n&quot;,buf);
printf(&quot;bytesread = %d, r2 = %d,count = %d \n&quot;,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 = &quot;c:\\dump.txt&quot;;

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, &quot;_SM_&quot;, 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 '&#9492; &#8597;'

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
 
'Unexpected End of File' means what compiller expects a }, a ; or something else, but not the end of file. I think you have an error in a header file. In my opinion you miss a ; after a class declaration in some header or something like that. Comment some #include header and compile again and you will see which one causes the error.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
It is not a compile time error, it is a run-time error. The 'Unexpected end of file' is a message I am printing in the Readtobuf() function in the following line

fprintf(stderr, &quot;%s: Unexpected end of file\n&quot;, prefix);

Guess I should have used a different message.
 
I suggest you the following approach for readtobuf:
Code:
HFILE hfFile = 0;
OFSTRUCT of;
DWORD ftr = GetFileAttributes(&quot;x:\path...\somefile&quot;);
if(ftr == -1)
{
    return 0;
}
hfFile = OpenFile(&quot;x:\path...\somefile&quot;, &of, OF_READ | OF_SHARE_DENY_NONE);
unsigned long sz = 0;
unsigned long hsz = 0;
unsigned long ret_sz = 0;
if(0xFFFFFFFF == (sz = GetFileSize((void*)hfFile, &hsz)))
{
    MessageBox(0, &quot;function GetFileSize(...)  
       falied&quot;, &quot;server debug message&quot;, 0);
}
query = new char[sz + 1];
query[sz] = 0;
ReadFile((void*)hfFile, query, sz, &ret_sz, 0);
CloseHandle((void*)hfFile);
you will read the whole file at once in a single buffer. There could be also many approaches.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
by the way, text filel has the symbol EOF which means the end of file. Binary files usualy does not have this symbol at the end.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Well, I did think of writing the whole file into a buffer. but then having a 64K buffer would be a bit huge.

Anyway as you say even if I try to read the whole file into a single buffer I will be reading only upto those three characters '¦¦ ¢Õ' and I will still get the same error.

By the way the file dump.txt actually is a dump of the bios portion of the memory. I am creating the dump.txt in 'w+b' mode which is binary. So will it help if I name it to something else like dump.bin or do I have to explicitly put an EOF char after writing to it.
 
the three char should have been '&#9492; &#8597;'
 
There could be the situation when you try to read for example 16 bytes, but until the end of file there are 10 bytes. I think you could handle this situation. See following pseudocode:


while(r2!=count && bytesread!=0)
{
remember position...;
bytesread=read(fd,buf+r2,count-r2);
if(bytesread==-1)
{
seek(fd, last_position...);
try to read again less characters;
}
else
r2+=bytesread;
}


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Yep, Idea is good and tried it. But, the code inside the

if(bytesread==-1){} structure is not executed as read() function is returning 0 to the bytesread variable.

This probably means that in the file read() is encountering an EOF character where it is not supposed to be.

Here are some values for two test dump.txt files I used.

bytesread = 0, r2 = 4,count = 16

bytesread = 0, r2 = 15,count = 16

So the issue now is how to differentiate the Flase EOF from the actual EOF.

Thanks for the code.
 
I think this character equals to -1, i.e. 0xFF. If you find this character in a textual file it means you have reached the EOF.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top