Seek na textovem souboru

Honza Pazdziora adelton na informatics.muni.cz
Pondělí Prosinec 17 11:12:38 MET 2001


On Mon, Dec 17, 2001 at 10:41:21AM +0100, Petr Horacek wrote:
> na Apachi pouzivam CGI psane v perlu. Jde mi o nasledujici kus kodu:
> 
> # $file = '../html/clanky/' . $FORM{kategorie} . '/nadpisy.txt';
> # open(SOUBOR, ">>$file");
> # seek(SOUBOR, 0, 0);
> # print SOUBOR "ahoj";
> # close(SOUBOR);
> 
> Ten by podle me mel na zacatek souboru vlozit text "ahoj", ale on ho stale
> vklada na jeho konec. V knize o C se pise, ze "seek" se pouziva na binarni
> soubory. Plati to i tady nebo pisu neco spatne?

Co plati v Perlu je dobre nehledat v knizkach o C, ale v dokumentaci
Perlu.

Tam se mimo jine pise (man perlfunc(1)):

             being created if necessary. If the filename begins
             with '>>', the file is opened for appending, again
             being created if necessary. You can put a '+' in
             front of the '>' or '<' to indicate that you want
[...]
             spaces.  These various prefixes correspond to the
             fopen(3) modes of 'r', 'r+', 'w', 'w+', 'a', and
             'a+'.

A jeste se podivame, cemu vlastne odpovida seek:

     seek FILEHANDLE,POSITION,WHENCE
             Sets FILEHANDLE's position, just like the fseek()
             call of stdio().  FILEHANDLE may be an expression

Cili az ted, kdyz vime, co vlastne Perl pod povrchem cela, prichazi
chvile na knizku o C, resp man fopen(3):

     Opening a file with append mode (a as the first character in
     the  mode argument) causes all subsequent writes to the file
     to be forced to the then current end-of-file, regardless  of
     intervening  calls  to  fseek(3S). If two separate processes

Voila!

-- 
------------------------------------------------------------------------
 Honza Pazdziora | adelton na fi.muni.cz | http://www.fi.muni.cz/~adelton/
   .project: Perl, DBI, Oracle, MySQL, auth. WWW servers, DBD::XBase.
------------------------------------------------------------------------


Další informace o konferenci Perl