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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EOF( ) in mid of FILE.......is'nt it funny?

Status
Not open for further replies.

malayaj

Programmer
Mar 19, 2001
11
Dear Friends..
My today's query on the first look may seem to be childish but it is troubling me a lot.
I want to ask you that "WHAT IS THE CHARACTER IN A FILE WHICH INDICATES End Of
File ?" And what is it's ASCII value?
Friends, I am working on a C project in which I have to read certain information from a file of Specific format. I have to read that file and extract some info.
I read the file using "while loop " which terminates on EOF( );
Now what heppens that when I read it some times (1 in 100 files) I notice EOF (value = -1)
In mid of the file.
And thus my program terminates although much data has left unread which makes all the things useless as I use that information for further processing.
IS IT THEORATICALLY POSSIBLE?
I tried to loopover that character in order to read other ones but that even did not work.
If you too have noticed this type of problem in your work then help me out..
regards
MALAYAJ
 
Everything is possible, especially when you have a bug in your code.
 
EOF isn't even necessarily a value in the file. In C, it's just an indication that the FILE pointer is at the end of the file and there is no more data to be read.

For example, in UNIX, files contain no actual EOF character.

You should post your code as it's probably a bug somewhere else.

Russ
bobbitts@hotmail.com
 
Dear Friends This is the sample of that file...which is troubling me it is named _pfc._ps
******************

00000052 öÿÿŸ Aã·:   testlinebreak :   00000053 õÿÿ¯ 1/2â·:  HGL REPORTS 9 T  00000054 ôÿÿ¿ ×ã·: 
Hi friends 5   00000055 óÿÿÏ U-¸:  ledger :   00000056 òÿÿß ø-¸: 
Yahoo! Mail 7 ü  00000069 øÿÿ÷,
, móŠ¸: 
My story
-------------------
And code is to extract id i.e no 0000 format 8 digit no
and corresponding name like testlinebrack
-------------------
My code to extract id is

void search::matchingobj(char* fname)//MATCH THE STRING IN
//THE FILE
{
int i;
int c;
int counter=0;
int ocrflag1=0;
int len1,len2,len3;
int npfcflag = 1;
int ocrflag2=0;
FILE *in;
int len[4];

if ((in = fopen(fname,"rt"))== NULL)
{
fprintf(stderr, "\nCannot open input file.\n");
exit(0);
}
while( ( c = getc(in) ) != EOF )
{ i = 0;
while( (c>=48 && c<=57) || (c>=65 && c<=70) ||c>=97&&c<=101))
{
wordbuf1=c;
c=getc(in);
i++;
}

if( i!=0 && (wordbuf1[0]!=48 && wordbuf1[1]!=48) )
{

for(int x=0;x<8;x++)
{
wordbuf[x] =NULL;
wordbuf1[x] = NULL;
}
i=0;
}
else
{
if(i!=0 && (wordbuf1[0]==48 && wordbuf1[1]==48) )
{
counter++;
for(int u=0;u<8;u++)
{
wordbuf=wordbuf1;
}

wordbuf[8]=NULL;

int length = strlen(wordbuf);

if(counter<4)
{
if(length==5)
{
ocrflag1=1;
ocrflag2=1;
pfcfind = 1;
}
len[counter] = length;
temppush(wordbuf);
}
else
{
if(ocrflag1==0)//normal pfc file
{
if(ocrflag2==0)
{
for(int f=1;f<4;f++)
{
push2(tempobjectid[f]);
}
ocrflag2 = 1;
}
push2(wordbuf);
}
else //pfc file comtaining pages i.e. ocrfile pfc
{

if(npfcflag==1)
{
if( (len[1]==len[2]) && (len[1]==8) )
{
push2(tempobjectid[2]);
push1(tempobjectid[3]);
counter--;
}

if(len[2]==5)
{
push2(tempobjectid[1]);
push1(tempobjectid[2]);
push2(tempobjectid[3]);
}
npfcflag = 0;
}

if(length ==5)
{
push1(wordbuf);
}
else
{
if(counter % 2 == 0)
{
pop2();
push2(wordbuf);
counter--;
}
else
{
push2(wordbuf);
}
}

}
}

for(int x=0;x<8;x++)
{
wordbuf[x]=NULL;
wordbuf1[x] = NULL;
}
}
}
}
if(counter == 1)
{
push2(tempobjectid[1]);
}

if(counter == 2)
{
if(len[1] == 8 && len[2] == 5)
{
push2(tempobjectid[1]);
push1(tempobjectid[2]);
}
if(len[1] == 8 && len[2] == 8)
{
push2(tempobjectid[1]);
push2(tempobjectid[2]);
}

}
if(counter == 3)
{
if(len[1] == 8 && len[2] == 8 && len[3] == 8)
{
push2(tempobjectid[1]);
push2(tempobjectid[2]);
push2(tempobjectid[3]);
}
if(len[1] == 8 && len[2] == 8 && len[3] == 5)
{
push2(tempobjectid[2]);
push1(tempobjectid[3]);
}
if(len[1] == 8 && len[2] == 5 && len[3] == 8)
{
push2(tempobjectid[1]);
push1(tempobjectid[2]);
}
}


fclose(in);
}
please bare with me as some code is formated to new line
due to lesser space here.

The above code is for extracting objectid of 00000052 form
There is similar code for extracting names. So I am not sending here.
****
CAN IT make some help if I read this file in BINARY MODE
instead of TEXT mode?
Please Help
regards
Malayaj
 
Yes, you should certainly be opening the file in binary mode.

in=fopen(fname,&quot;rb&quot;);

I didn't look at the rest of your code because it was starting to hurt my eyes :) ... next time you post code, make sure you uncheck the box &quot;Process TGML&quot; or enclose your code in the
Code:
tags.

Russ
bobbitts@hotmail.com
 
Rbobbit is right. The best way is to open the file in binary mode, and then read chunks of data from the file with read function, which returns the number of bytes read. If that number is smaller then the buffer size you specified, you can be preety sure that you found the end of file. That is, if you do not read the file via network connection or something, when read function can read less than what you expect due to delays in the net.

So, try to find the filesize (using stat) and then count the number of bytes read, untill you reach the filesize.
This should work...
 
Hi all,
thanks all for your valueavle suggestions...
Reading file in binary mode made whole the trick.
Thank you again
regards
Malayaj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top