Reputation: 4897
#include<stdio.h>
int main()
{
FILE *fp,*fs;
fs=fopen("c.dat","w");
fp=fopen("c.dat","w");
putc('a',fp);
putc('1',fs);
close(fs);
close(fp);
return 0;
}
Irrespective of the order execution
putc('a',fp);putc('1',fs);
putc('1',fs);putc('a',fp);
contents of c.dat is '1' only. Can somebody explain the reason ?
Upvotes: 0
Views: 176
Reputation: 27486
You didnt specify the unerlying OS, but on win and *nix systems file buffers are shared between processes, so :
putc('a',fp);
putc('1',fs);
are writing to exactly the same address in the same buffer if you reversed the order of the putc statements you will end up with an 'a' in the file.*
Just goes to show how tricky this all is. I have tried this and what affects the output is the order in which the files are opened -- the first open always takes precedence.
Upvotes: 1
Reputation: 882606
For a start, you don't close()
a file that you opened with fopen()
, you have to fclose()
it. You're mixing two levels of abstraction here, file handles and file pointers.
On top of that, I think you'll find that the two file pointers are maintaining independent file offsets and buffers. Whichever one closes the file last will write their data last, overwriting the other.
Change the order of your fclose()
statements and you should see what I mean:
#include<stdio.h>
int main (void) {
FILE *fp,*fs;
fs=fopen("c.dat","w");
fp=fopen("c.dat","w");
fputc('a',fp);
fputc('1',fs);
fclose(fp); // Try changing these
fclose(fs); // two around.
return 0;
}
I did that and consistently got the contents based on the last file closed.
And, before you say that you should have gotten a
because fp
was the last one you closed, you did not close fp
(with an fclose()
).
What you did do was try to close()
a file handle that was almost certainly invalid, because file handles are generally relatively small values (e.g., 0
, 1
, 2
) while file pointers are generally relatively large values (e.g., 0xdeadbeef
, 0xcaf3f00d
).
So the closure of both those files was left up to the C runtime environment when you exited (and that would most likely have done it in a deterministic way, which is why you always got the same value in the file).
Upvotes: 2
Reputation: 3423
I think it is because you opened c.dat through fs=fopen first and then some lock is not allowing "a" to be written, as it would require the file to be opened again.
Upvotes: 0
Reputation: 20724
You are opening the same file for writing twice. All bets are off at that point.
Upvotes: 1