Re: [PHP] Append to the beginning of a file...

2002-03-22 Thread Chris Hewitt

Mauricio,

If you are on a *nix computer, you could append to the end of the file, 
close it and then do a tail -n1 which will give you just the last line 
in the file.

HTH.
Regards

Chris

Mauricio Sthandier wrote:

I guess I should do that.
I need the plain file because is just a log of users registering in a Club,
in a csv format.
Using a database is just too much and not intended for this page... even
when I would prefer using mysql. At least, I can handle that rather than the
fopen and fwrite !.
I could append but then I need to read the first 8 chars of the last line I
appended... neither something I know how to do.
If they were fixed I could... maybe I can fill it with blank chars the line
until a given size ?

Thanxs everyone for your replies.

Miguel Cruz [EMAIL PROTECTED] escribi en el mensaje
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...

On Thu, 21 Mar 2002, Mauricio Sthandier wrote:

I guess thist must be really silly... but how can I append text to the
beginning of a file ???
I've been trying since a while... but all I get is overwriting !.

Open a temporary file, write your new data to it, copy the old file on to
the end of it, remove the old file, rename the temporary file with the old
file's name.

Obviously this is not very efficient. Explore whether you really need to
prepend. Perhaps you can append and then read the file backwards a la tac?
Or perhaps a database is more useful. Or multiple files.

miguel








-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Append to the beginning of a file...

2002-03-21 Thread Mauricio Sthandier

I guess thist must be really silly... but how can I append text to the
beginning of a file ???
I've been trying since a while... but all I get is overwriting !.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Append to the beginning of a file...

2002-03-21 Thread Mauricio Sthandier

I already seen it... I used r+ and aw.
I saw and example in www.php.net, in the fopen function description, but it
is incomplete...
this is taking me hours !!!.
Well, if I can preappend... how can I read, for example, the first 8 chars
of the last line I inserted ?. They have no fixed length.
I'm starting to feel silly :(.

Chris Boget [EMAIL PROTECTED] escribió en el mensaje
news:01bd01c1d0f6$3fc51f60$[EMAIL PROTECTED]...
  I guess thist must be really silly... but how can I append text to the
  beginning of a file ???
  I've been trying since a while... but all I get is overwriting !.

 Check out fopen().  There is an argument that you can use in that function
 call to determine how the data is sent to the file.

 Chris




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Append to the beginning of a file...

2002-03-21 Thread Miguel Cruz

On Thu, 21 Mar 2002, Mauricio Sthandier wrote:
 I guess thist must be really silly... but how can I append text to the
 beginning of a file ???
 I've been trying since a while... but all I get is overwriting !.

Open a temporary file, write your new data to it, copy the old file on to 
the end of it, remove the old file, rename the temporary file with the old 
file's name.

Obviously this is not very efficient. Explore whether you really need to
prepend. Perhaps you can append and then read the file backwards a la tac?  
Or perhaps a database is more useful. Or multiple files.

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Append to the beginning of a file...

2002-03-21 Thread Jason Wong

On Friday 22 March 2002 00:40, Mauricio Sthandier wrote:
 I already seen it... I used r+ and aw.
 I saw and example in www.php.net, in the fopen function description, but it
 is incomplete...
 this is taking me hours !!!.
 Well, if I can preappend... how can I read, for example, the first 8 chars
 of the last line I inserted ?. They have no fixed length.
 I'm starting to feel silly :(.

 Chris Boget [EMAIL PROTECTED] escribió en el mensaje
 news:01bd01c1d0f6$3fc51f60$[EMAIL PROTECTED]...

   I guess thist must be really silly... but how can I append text to the
   beginning of a file ???
   I've been trying since a while... but all I get is overwriting !.
 
  Check out fopen().  There is an argument that you can use in that
  function call to determine how the data is sent to the file.

Write your stuff to a new file, 
read old file, 
add stuff from old file onto new file,
rename new file to old file


OR

read old file into a very long string
add new stuff to beginning of very long string
write very long string to old file

-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk

/*
Technological progress has merely provided us with more efficient means
for going backwards.
-- Aldous Huxley
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Append to the beginning of a file...

2002-03-21 Thread Mauricio Sthandier

I guess I should do that.
I need the plain file because is just a log of users registering in a Club,
in a csv format.
Using a database is just too much and not intended for this page... even
when I would prefer using mysql. At least, I can handle that rather than the
fopen and fwrite !.
I could append but then I need to read the first 8 chars of the last line I
appended... neither something I know how to do.
If they were fixed I could... maybe I can fill it with blank chars the line
until a given size ?

Thanxs everyone for your replies.

Miguel Cruz [EMAIL PROTECTED] escribió en el mensaje
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 On Thu, 21 Mar 2002, Mauricio Sthandier wrote:
  I guess thist must be really silly... but how can I append text to the
  beginning of a file ???
  I've been trying since a while... but all I get is overwriting !.

 Open a temporary file, write your new data to it, copy the old file on to
 the end of it, remove the old file, rename the temporary file with the old
 file's name.

 Obviously this is not very efficient. Explore whether you really need to
 prepend. Perhaps you can append and then read the file backwards a la tac?
 Or perhaps a database is more useful. Or multiple files.

 miguel




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Append to the beginning of a file...

2002-03-21 Thread Miguel Cruz

On Thu, 21 Mar 2002, Mauricio Sthandier wrote:
 I could append but then I need to read the first 8 chars of the last line I
 appended... neither something I know how to do.

Seek to the end of the file minus typical record length, read, 
look for the penultimate end-of-record delimiter (\n, probably) in there, 
and you've found your final record. If you don't find a delimiter, read 
some more and tack it onto the beginning of your string, then check again. 
Repeat until batter contains no large lumps.

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Append to the beginning of a file...

2002-03-21 Thread Mauricio Sthandier

Well, here's a lousy solution to a lousy problem :
(please don't crucify my code since I know it's not depurated)


---
$archivo = fopen(./registrados.txt, a+);
if ($archivo) {

$posicion = fseek($archivo, -512, SEEK_END);
$datos = fread($archivo, 7);
$entero = intval(substr($datos, 2, 5));
If (empty($entero) || !is_int($entero)) {
$entero = 1000;
}
$entero++;
$socio = str_repeat(0, 5 - strlen(strval($entero))).strval($entero);

$linea = \n\$socio\;\$fecha\;\$nombre
$apellido\;\$rut\;\$fono\;\$direccion\;\$comuna\;\$ciudad\;\$pai
s\;\$edad\;\$email\;\$select_familiares\;\$select_lugar_compra\;\$
cocina\;\$select_personas_cocinan\;\$recetario\;
$linea = $linea . str_repeat( , 512 - strlen($linea));
fseek($archivo, 0, SEEK_END);
fwrite ($archivo, $linea, 512);
fclose($archivo);

-

It does works... sounds amazing after this hours... helps everybody, you are
fine people !!!



Miguel Cruz [EMAIL PROTECTED] escribió en el mensaje
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 On Thu, 21 Mar 2002, Mauricio Sthandier wrote:
  I could append but then I need to read the first 8 chars of the last
line I
  appended... neither something I know how to do.

 Seek to the end of the file minus typical record length, read,
 look for the penultimate end-of-record delimiter (\n, probably) in there,
 and you've found your final record. If you don't find a delimiter, read
 some more and tack it onto the beginning of your string, then check again.
 Repeat until batter contains no large lumps.

 miguel




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php