Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I am facing some problem while opening a file in " a+" mode. according to the a+ mode. it should open for reading and writing (append if file exists) and starts at the end of file. but when ever i am checking for EOF it Asserts. please check the below code.
C++
FILE *pFILE = fopen("c:\\TEST.txt", "a+");
ASSERT(0 != feof(pFILE));
fclose(pFILE);
pFILE = 0;

can anybody help me in understanding this.

Thanks in advance.
Posted
Updated 28-Jul-11 21:25pm
v3

Try
fseek ( pYFILE ,0 , SEEK_SET );
before assert.

As it is in append mode as well the file pointer will be at the end of the file.
 
Share this answer
 
v2
Comments
sourabhmehta 29-Jul-11 3:43am    
yes, i tried this. i got a strange behavior, still that asserts in 'a+' mode..
Without knowing what YFILE and yfopen etc. actually do, it is just guesswork.
But, I can't help thinking that just from the names pYFILE should be a YFILE* rather than a YFILE

Don't think that will fix your problem, but it looks like you have some explaining to do...
Use the "Improve question" widget to edit your question and provide better information.


FILE *pFILE = fopen("c:\\TEST.txt", "a+");
ASSERT(0 != feof(pFILE));


feof[^] "The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file."

You haven't done a read operation, so feof will return zero.
 
Share this answer
 
v2
Comments
sourabhmehta 29-Jul-11 3:44am    
yes, I modified it...Please check if you can answer it
OriginalGriff 29-Jul-11 4:17am    
Answer updated
The code mearly cleans up after opening the file for read and write mode (append if the file exist).

Thus:
1) open the file for reading and writing append mode.
2) Check whether EOF... Looks like some code missing. ALSO: are you sure its yfeof
and not feof
3) close the stream.
4) Nullify pointer.

I would use the ASSERT in a while loop otherwise this code seems pointless.
E.g.

while ( ASSERT(0 != feof(pYFILE)) )
{
count = fread( buffer, sizeof( char ), 100, stream );
total += count;
}
 
Share this answer
 
Comments
sourabhmehta 29-Jul-11 3:41am    
First of all Thanks for the response,

Since the description of the 'a+'mode says.It will always starts at the end..so i just want to check that whether the given description is true or not....or you can assume it as a testing of the particular mode that is the reason i am not reading anything...i just want to validate the operation performed by 'a+' mode...
PrafullaVedante 29-Jul-11 4:29am    
bang bang ...... @R.Eramus

Use ASSERT for the validation puprose at the development time only.
Using it in "while" or any decision taking statement should be avoided.

Adding and removing ASSERT should not change the behaviour of your program.This macro is designed to capture programming errors, not user or running errors
MSDN states:
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete


So the assertion behavior is normal.

If you don't want EOF to be removed, then use "a" instead of "a+".
 
Share this answer
 
v2
Comments
sourabhmehta 29-Jul-11 7:07am    
Thanks Olivier

Your description really makes sense :)
sourabhmehta 29-Jul-11 7:34am    
Do you mean, If after opening a file ..if i write something to the file and then try to Assert the same..it wont Assert....I tried that..still it is asserting
Olivier Levrey 29-Jul-11 8:39am    
No I think, EOF will be put only after closing the file (I am not sure of that but it is what I understand from the description).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900