Problem s close()
Jan Kasprzak
kas na informatics.muni.cz
Středa Červenec 15 14:04:21 CEST 1998
Eduard Omasta pise:
: close(1);
: creat("pom",00700);
: printf("abc");
: close(1);
: Ak v programe zmazem druhe "close(1)", tak vytvoreny
: subor "pom" skutocne obsahuje retazec "abc".
: Preco povodny program nevytvori subor s retazcom "abc"?
Nesmite mixovat pristup pres deskriptory (open, close, creat, write)
s pristupem pres filehandle (fopen, fclose, [f]printf, ...). Pomoci printf
sice ulozite nejaky text do bufferu _filehandle_ stdout, ale pokud pred
vylitim tohoto bufferu (pri ukonceni programu nebo pri fflush(stdout) nebo
pri fclose(stdout)) uzavrete _deskriptor_ tohoto filehandle pomoci close(),
nic se samozrejme ven nevylije.
Spravne by bylo
FILE *fp = fopen ("pom", "w");
if (fp) {
fprintf(fp, "abc");
fclose(fp);
}
nebo
close(1);
if (creat("pom", 0700)) >= 0) {
write(1, "abc", 3);
close(1);
}
-Yenya
--
\ Jan "Yenya" Kasprzak <kas at fi.muni.cz> http://www.fi.muni.cz/~kas/
\\ PGP: finger kas at aisa.fi.muni.cz 0D99A7FB206605D7 8B35FCDE05B18A5E //
\\\ Czech Linux Homepage: http://www.linux.cz/ ///
If there are race conditions in programs fix them. The "my programs suck fix
something else" mentality leads you to things like Java. -- Alan Cox
Další informace o konferenci Linux