I am using filepointer in C. when I compile the code
and run it for the first time the filepointer works fine and
logs are written into log files. But during next hits there is an
inconsistency in logs getting written. sometimes getting written
and someother times not written. But during all those times
the file pointer returns a valid integer.

So I tried to close the file pointer and opened again before writing into logs now it works fine during all execution.

Can anybody explain me what was the exact problem, how can
I fix that without closing and opening again as it is occuring at
some time and not in an inconsistent way.

Its normally a rule of the thumb to close the file pointer as soon as you are finished with it to avoid the undefined behaviour of the file stream which you have opened. Even if you dont close the file stream, they are automatically closed when your program exits but is regarded as a bad practice and may cause subtle bugs due to the corruption of the open file stream. Hence better close the file stream as soon as you are done wiht it rather than waiting for them to be automatically closed.

And btw file poitner returning valid integer as you say means that the file pointer exists and is pointing to the file currently opened but doesnt necessarily guarantee defined behaviour.

Hope it helped, bye.

Thanks SOS,

So you mean to say even if the file pointer points to the file,
it may not write into it at times..as it was kept opened.

It is not called not writing into the files, as mentioned by me earlier, it is called undefined behaviour when the file stream is kept open.

Undefined behaviour can imply successful write as well as an unsuccessful write, but cant be determined when that happens. Use the funtion int ferror (FILE* file_ptr) to check whether the file stream is corrupted or not.

HOpe it helped, bye.

Its normally a rule of the thumb to close the file pointer as soon as you are finished with it to avoid the undefined behaviour of the file stream which you have opened.
----
It is not called not writing into the files, as mentioned by me earlier, it is called undefined behaviour when the file stream is kept open.

Undefined behaviour can imply successful write as well as an unsuccessful write, but cant be determined when that happens. Use the funtion int ferror (FILE* file_ptr) to check whether the file stream is corrupted or not.

I'm not sure what you are saying here. It looks like you are saying if a file is not closed you enter the realm of undefined behavior. This is the first I've heard this. Can you point out something that substantiates this view?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.