★ wanayoo — archive 1999 http://www.devshed.com/Talk/Forums/Forum5/HTML/002252.htmlNouvelle recherche | Portail wanayoo
DevShed Menu
* DevShed Home
* Developer News
* DevShed Brain Dump




UBBFriend: Email This Page to Someone!
  DevShed Discussion Forums
  PHP
  file pointer at beginning of file....

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   file pointer at beginning of file....
ROT
Junior Member
posted August 02, 2000 06:09 PM     Click Here to See the Profile for ROT   Click Here to Email ROT     Edit/Delete Message Reply w/Quote
The following is taken directly from the PHP quick ref section under fopen.

'r' - Open for reading only; place the file pointer at the beginning of the file.

'r+' - Open for reading and writing; place the file pointer at the beginning of the file.

'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

None of these do what I want though one of them say they will. I need to write input from a form to the beginning of a text file. But when I use

$myFile = fopen("file.txt","r+");

it writes to the beginning of the file. But when I execute the script again it writes OVER what was previously there. I need it to INSERT the input from the form. Please help!

I'm fairly new to PHP so if I've not included some valuable information let me know.

Regards,
Justin Southern

Shiju Rajan
Member
posted August 02, 2000 08:18 PM     Click Here to See the Profile for Shiju Rajan   Click Here to Email Shiju Rajan     Edit/Delete Message Reply w/Quote
i think you are try to append the values to the file..If you want to append values to an existing file.then you can use

$myFile = fopen("file.txt","a");

------------------
SR -
webshiju.com

"The fear of the LORD is the beginning of knowledge..."

ROT
Junior Member
posted August 03, 2000 12:05 AM     Click Here to See the Profile for ROT   Click Here to Email ROT     Edit/Delete Message Reply w/Quote
Yeah, that's what I'm using now. BUT, I want to INSERT at the BEGINNING of the text file. None of the parameter's I've tried accomplish this. Is there one that I'm missing?

freebsd
Member
posted August 03, 2000 01:00 AM     Click Here to See the Profile for freebsd     Edit/Delete Message Reply w/Quote
>>Is there one that I'm missing?

Ya know what a newline is -> \n

ROT
Junior Member
posted August 03, 2000 01:24 AM     Click Here to See the Profile for ROT   Click Here to Email ROT     Edit/Delete Message Reply w/Quote
quote:
Originally posted by freebsd:
Ya know what a newline is -> \n


Not quite sure I follow? What does that have to do with writing information to the beginning of a text file without writing over other information?

freebsd
Member
posted August 03, 2000 01:32 AM     Click Here to See the Profile for freebsd     Edit/Delete Message Reply w/Quote
>>Not quite sure I follow?

This is a common sense and very basic. Okay, let say you open a new text document with notepad, type something, then press ENTER, what does it do? then you are on the 2nd line, that is, the file pointer is now at your 2nd line. So the whole idea is to go downwardly, not upwardly as you thought.

the newline ( \n ) is to automated the task of pressing ENTER. So your new record is following line by line from top to bottom. If you set the filepointer to the first line and enter something, of course, your existing first line will get overwritten. And, that first line will NOT move to second line automatically.

[This message has been edited by freebsd (edited August 03, 2000).]

ROT
Junior Member
posted August 03, 2000 01:39 AM     Click Here to See the Profile for ROT   Click Here to Email ROT     Edit/Delete Message Reply w/Quote
But I want to INSERT the information at the beginning. Say I have 3 lines of information to write to a text file and there are alread lines of text in the file. I want these 3 new lines of text to be INSERTED. Is that not possible? Can I only append?

freebsd
Member
posted August 03, 2000 01:53 AM     Click Here to See the Profile for freebsd     Edit/Delete Message Reply w/Quote
>>But I want to INSERT the information at the beginning

Then your existing lines will get overwritten. It's all about file pointer, the existing lines don't move down automatically when there are new lines appending.

>>I want these 3 new lines of text to be INSERTED.

Once again, if you want to keep your existing lines in that file, use 'a'.

>>Is that not possible?
It's possible, you can do something before or after inserting your lines, like reading the existing lines and put it in array, then insert the new lines, then put the array back after your new lines, but your logic/idea makes it impossible.

sano
Member
posted August 03, 2000 02:04 AM     Click Here to See the Profile for sano   Click Here to Email sano     Edit/Delete Message Reply w/Quote
Do what FreeBSD says to do..

Read the file into an array and then add the line you want to be in the beginning of the file to the last cell in the array, then print the array to a file in descending order using something like this:

for ($i=count($array); $i>-1; $i--){
write to file
}

Note that this is only an algothrim to give an example. I am not sure if it will work the way it is set up.

Shiju Rajan
Member
posted August 03, 2000 02:50 PM     Click Here to See the Profile for Shiju Rajan   Click Here to Email Shiju Rajan     Edit/Delete Message Reply w/Quote
ROT,
first you open the file in read|write mode.ie ,"r+".
then use rewind() to insert your values at the beginning of file(ie,rewind($fp)).

---
probably you can check it out ftell() and fseek() functions also.
----
see the manual for the details...


------------------
SR -
webshiju.com

"The fear of the LORD is the beginning of knowledge..."

All times are MST (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | DevShed.com

Copyright © 1997-2000 ngenuity. All rights reserved.

Powered by: Ultimate Bulletin Board, Version 5.41a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 1999.