Jump to content


Photo

FOPEN other options outside R W A....


  • Please log in to reply
4 replies to this topic

#1 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 08 May 2006 - 06:13 PM

What I'm wanting to do sounds very simple but the options I know of and have searched for haven't lead to an answer yet.

Basically I'm trying to find a method within FOPEN, to just replace a character or number within the line. I know W (write) would simply write over the existing information, and A (append) would just add to the file at the end. Is there some happy medium where I can change say one character or number within the file and leave the rest of the file as is?



#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 May 2006 - 06:25 PM

If you want to edit something in a file then you'll want to get the contents of the file, apply the changes and then rewrite the file.

You will want to use fopen, fread, str/preg_replace or other functions and then fwrite and fclose.

#3 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 08 May 2006 - 06:30 PM

Thanks but if I use say:

fopen( $filename, "a" )

It would ONLY add to the end, and if I use "w" it would rewrite the whole file. What instead would I use so I can edit within the file itself?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 May 2006 - 06:55 PM

You cannot edit inside a file you can only append to or rewrite the whole entire file.

That is why I said to use [a href=\"http://uk.php.net/manual/en/function.fread.php\" target=\"_blank\"]fread[/a] which you can then you add the contents of the file into a variable. Then use str/preg_replace to do the changes and use fwrite to rewrite the file with changes.

For example you have a file called test.txt with the following text init:
this sentance has a mistake in it
Now you want to change the sentace to something else. You will do that by doing the following:
[a href=\"http://pastebin.com/705891\" target=\"_blank\"]click to see code[/a]

#5 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 08 May 2006 - 07:28 PM

Thanks for the help. I'm defantly on the right track now :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users