visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Algorithms :: General :: INSERTAR LINEAS PARA MODIFICAR UN ARCHIVO DE TEXTO CON PHP


By: Gianos Other  Date: 04/06/2007 16:53:37  Spanish English  Points: 0 Status: Answered
Quality : Excellent
Buenos dias, necesito saber como puedo insertar lineas en un archivo de texto usando php, hasta ahora solo he podido insertarlas al final del archivo, pero debo insertarlas en cualquier lugar del archivo.
By: Bernard Date: 10/06/2007 09:06:15 English  Type : Answer
[transl]
Good morning, I need to know as [how] I can insert line in a text file using php, until now single I have been able to insert them at the end of the file, but I must insert them in any place of the file.
[/transl]

sorry to reply in English, I can't write Spanish down.

Your problem is rather classical.

Using text files, from Turbo Pascal 3 and even older BASICs, has relied on using this mecanism :
- open with a given mode (read/write/append)
- opening in append or write created the file if it doesn't exist
- move to a given location [either by line counting, or bytes counting, or at EOF or BOF]
- write or read at the current position
- at the end, close the file
- you may have an explicit flush() to solve buffering problems & R/W delays.
- you can't usually read AND write to the same file, first because if only one is opened, it's in only ONE mode ; second, because if you open the same file twice (one in 'r', the other in 'w' mode) the OS should LOCK the file and/or complain about writing to a reserved file.

This stated, you've all you need to solve your problem ; I see it this way :

- open file#1 ( $filepointer=fopen('filename','r'); )
- open file#2 - different name like 'filename.new', mode 'w' - will create the file
- read lines from file#1, write down to file#2 until you've found your insert point
- insert the line in file#2
- resume on copying : read lines from file#1, write down to file#2 until you've found EOF (end of file) on file#1
- close file#2 (essential), close file#1 (good practice)
- done

this mecanism reproduces what OS level commands do. You've no other solution.

This can be accelerated a lot if you've something like UCSD Pascal's BlockRead() and BlockWrite() and you can compute precisely the octets (bytes) position you need to reach in file#1 before inserting :
- read in one operation as many bytes as necessary using BlockRead() in file#1
- BlockWrite() to file#2
- insert new data in file#2
- read the remainder of bytes as necessary using BlockRead() from file#1
- BlockWrite() to file#2
- close files

2 read operations, 3 write operations ;-)

ask for more help if need be.

best regards
By: VGR Date: 10/06/2007 09:10:57 English  Type : Comment
yes, agree.

You had inserted your question in "feedback", so it appeared closed because feedback entried do not usually require an answer, so it was not considered as a "new open question" and was in fact almost invisible, hence the delay in answering from our Experts.

Algorithmic questions like this are better placed in Algorithms/General or if you've a specific language requirement, in the relevant Languages section.

ttyl
By: VGR Date: 15/06/2007 15:10:55 English  Type : Comment
ok ? got a good answer ?
By: OpConsole Date: 04/07/2007 16:53:16 English  Type : Comment
Dear,

If you found some of the above comments to have proved helpful in solving your issue, you shall Accept the Answer or sPlit points between the various useful comments. Each one can receive a quality evaluation from + (somewhat helpful) to +++ (working solution).

Given this Question has been Open for quite a while now, please accordingly "accept an Answer" ASAP

This Question will be forced-closed in one month from now.

Thanks and regards.

Admin.
By: OpConsole Date: 04/07/2007 16:53:53 Spanish  Type : Comment
Estimado, Si encontraste algunos de los comentarios antedichos para tener provechoso probada en solucionar tu edición, aceptarás la respuesta o los puntos partidos entre los varios comentarios útiles. Cada uno puede recibir una evaluación de calidad de + (algo provechoso) a +++ (solución de trabajo). Se ha dado esta pregunta abierto por absolutamente un rato ahora, “aceptan por favor por consiguiente una respuesta” CUANTO ANTES Esta pregunta será forzado-cerrada en un mes de ahora en adelante. Agradece y respeto. Admin.

Do register to be able to answer

EContact
browser fav
page generated in 369.050030 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page