Jump to content

Can fwrite() write into, rather over?


PlayItAgain

Recommended Posts

Hi all,

 

I have fwrite() writing to a text file called 'numbers.txt'. The text file content is "23".

 

I learn't fwrite() from the manual, so did:

f$fp = fopen('/opt/numbers.txt', 'w');
fwrite($fp, '1');
fclose ($fp);

 

I have used fwrite() to try and make the file content "123", but fwrite() writes over the content of the file, resulting in it just containing "1".

 

I couldn't find anywhere about writing into, rather over, and hoping someone could please help me out.

Link to comment
Share on other sites

Look at the manual for fopen to see what the different modes do.  'w' is:

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.

 

In otherwords, it erases the entire contents of the file when you open it.  There are other modes that allow writing which do not do this, such as 'r+' or 'a'.

 

That said, there is no 'insert' mode where you can write data to the beginning (or middle) of a file without overwriting the data which is there.  If you want to do that you have to read in all the existing data to a variable, truncate the file, write the new data, then re-write the old data.

 

You can append data to the end of a file without having to touch the old data first.  Use mode 'a' and then just fwrite the new data to put it at the end.

 

 

Link to comment
Share on other sites

Look at the manual for fopen to see what the different modes do.  'w' is:

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.

 

In otherwords, it erases the entire contents of the file when you open it.  There are other modes that allow writing which do not do this, such as 'r+' or 'a'.

 

That said, there is no 'insert' mode where you can write data to the beginning (or middle) of a file without overwriting the data which is there.  If you want to do that you have to read in all the existing data to a variable, truncate the file, write the new data, then re-write the old data.

 

You can append data to the end of a file without having to touch the old data first.  Use mode 'a' and then just fwrite the new data to put it at the end.

 

yes you are right. using 'a' mode is the way to point the pointer at the end. But my question is how to do a break line ??

 

check my code.

 

<?php
$handle = fopen("test.txt",'a');
//Writing a new line.
$result = fwrite($handle,"My name is sonu");
if($result) { echo "Done"; } else { echo "error"; }
fclose($handle);
?>

 

the file test.txt has already a line of text that is "my name is khan" & after executing the above code the text file contain a line like this

 

my name is khanMy name is sonu but i want to make it like this

 

my name is khan

My name is sonu

 

How it is possible ??

 

Link to comment
Share on other sites

I am going to give this shit 1 last shot.

I would appreciate it if people stop giving me fuckin manual pages. I can't understand them if it hasn't become obvious by now.

 

I have so far been posting basic versions of what I am trying to do. To gain away any misunderstanding, I am gonna post my real code.

You guys seem to know whats going on. So I don't think it will be a strain for your to read (I thought this maybe before which is why I made it basic).

 

$fp = fopen('/opt/index.html', 'r+');
fwrite($fp, '<?php
session_start();
$id = "'.mysql_real_escape_string($_POST['id']).'";');
fclose ($fp);

This inserts into the following index.html:

 

testusr', 'testpw') or exit("SERVER ERROR");
mysql_select_db('testdb', $link);
$query = "SELECT * FROM products WHERE id = '$id'";
...
...
...

 

I am trying to make the outcome:

<?php
session_start();
$id = "21";
$link = mysql_connect('testusr', 'testpw') or exit("SERVER ERROR");
mysql_select_db('testdb', $link);
$query = "SELECT * FROM products WHERE id = '$id'";
...
...
...

 

Instead, I get:

<?php
session_start();
$id = "21";
testusr', 'testpw') or exit("SERVER ERROR");
mysql_select_db('testdb', $link);
$query = "SELECT * FROM products WHERE id = '$id'";
...
...
...

 

See how fwrite is eating into the code.

I need this to stop. This is what all of this is about.

 

So, I am trying to line break to get it going to the next line. But I can't! Its driving me nuts!

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.