473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I prevent fwrite() overwriting text

Hello!

I have this problem, when fwrite() writes to the beginning of xyz.txt
file, it overwrites the first line. Any ideas how to prevent this? I'm
running PHP 4.3.3RC3 on Linux.

<?php
$fp = fopen("xyz.txt","r+");
fwrite ($fp,"$newline");
fclose ($fp);
?>

-Tuuska
Jul 17 '05 #1
3 11545
Tuuska wrote:

Hello!

I have this problem, when fwrite() writes to the beginning of xyz.txt
file, it overwrites the first line. Any ideas how to prevent this? I'm
running PHP 4.3.3RC3 on Linux.

<?php
$fp = fopen("xyz.txt","r+");
fwrite ($fp,"$newline");
fclose ($fp);
?>

-Tuuska


You could try appending ( use "a" instead of "r" ). That'll put you new text at
the end of the file. If it has to do at the beginning, you'll have to read the
file into memory, write your new text, then write the rest of the file from
memory. Or you could open a new, temporary file, write to it, read your
existing file and write to the temp file chunk by chunk. Then copy the temp
file over the existing file.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #2
Shawn Wilson <sh***@glassgiant.com> wrote in message news:<3F***************@glassgiant.com>...
Tuuska wrote:

Hello!

I have this problem, when fwrite() writes to the beginning of xyz.txt
file, it overwrites the first line. Any ideas how to prevent this? I'm
running PHP 4.3.3RC3 on Linux.

<?php
$fp = fopen("xyz.txt","r+");
fwrite ($fp,"$newline");
fclose ($fp);
?>

-Tuuska


You could try appending ( use "a" instead of "r" ). That'll put you new text at
the end of the file. If it has to do at the beginning, you'll have to read the
file into memory, write your new text, then write the rest of the file from
memory. Or you could open a new, temporary file, write to it, read your
existing file and write to the temp file chunk by chunk. Then copy the temp
file over the existing file.

Shawn


Hello!

Thanks for your tip! I managed to make it work! First it reads the
content of txt file into $contents and then writes $newline and
eventually writes $contents to the end of the txt file.

Probably not the best way to do this, but it's enough for me.

<?php
$filename = "xyzt.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
?>

<?php
$fp = fopen("xyz.txt","w+");
fwrite ($fp,"$newline");
fclose ($fp);
?>

<?php
$fp = fopen("xyz.txt","a");
fwrite ($fp,"$contents");
fclose ($fp);
?>

-Tuuska
Jul 17 '05 #3
Tuuska wrote:

Shawn Wilson <sh***@glassgiant.com> wrote in message news:<3F***************@glassgiant.com>...
Tuuska wrote:

Hello!

I have this problem, when fwrite() writes to the beginning of xyz.txt
file, it overwrites the first line. Any ideas how to prevent this? I'm
running PHP 4.3.3RC3 on Linux.

<?php
$fp = fopen("xyz.txt","r+");
fwrite ($fp,"$newline");
fclose ($fp);
?>

-Tuuska
You could try appending ( use "a" instead of "r" ). That'll put you new text at
the end of the file. If it has to do at the beginning, you'll have to read the
file into memory, write your new text, then write the rest of the file from
memory. Or you could open a new, temporary file, write to it, read your
existing file and write to the temp file chunk by chunk. Then copy the temp
file over the existing file.


Thanks for your tip! I managed to make it work! First it reads the
content of txt file into $contents and then writes $newline and
eventually writes $contents to the end of the txt file.

Probably not the best way to do this, but it's enough for me.


Just a few tips/cautions. The way it's written now should work fine as long as
the original file is small and will stay small. You may run into trouble if you
get a very large file.

Depending on the application, you may want to consider making parts 2 and 3
conditional upon part 1 being completed successfully. Otherwise the original
file will not be written into the new file - you'll just have $newline in there.

There's really no need to close the file at the end of part 2 and reopen it at
the beginning of part 3. Those could be done with a single fopen() and 2
fwrite()s. It may be that you've done it this way because some code that you've
snipped out requires it. If so, ignore this.

You may want to consider changing your coding to something like the following
(UNTESTED):

$filename = "xyzt.txt";
if ($fd = @fopen ($filename, "r")) {
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
}
else
echo("Could not open the file <i>$filename</i>");

Otherwise, if there's an error opening the file, it will be displayed, along
with an error for the fread, and an error for the fclose(). While you're
testing, though, don't use the "@". You want errors displayed while you're
testing.

//PART 1 <?php
$filename = "xyzt.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
?> //PART 2 <?php
$fp = fopen("xyz.txt","w+");
fwrite ($fp,"$newline");
fclose ($fp);
?> //PART 3 <?php
$fp = fopen("xyz.txt","a");
fwrite ($fp,"$contents");
fclose ($fp);
?>


Regards,
Shawn

--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Lisa Pearlson | last post by:
Hi, I have a php script with no more than this: <?php echo "Hello World!"; ?> When a webbrowser client requests data, it receives Apache server headers, followed by my data: HTTP/1.1 200...
10
by: Bobby T | last post by:
Here's the problem, when I try to append an RDP file (which is written in hex but readable in notepad) it adds funny little squares to the file. I have tried opening the file in every mode but...
1
by: Chance Lerro | last post by:
Here's another way to PREVENT the install program for run-time Access from overwriting existing file associations: 1. Edit the Runtime MSI file with Orca, InstallShield, etc.. 2. Go to the...
23
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
15
by: Suraj Kurapati | last post by:
Hello, I'm having a rather strange bug with this code: for certain values of 'buf', a segmentation fault occurs when 'free(buf)' is followed by an 'fwrite()'. In the program output, there is no...
4
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
10
by: Sheldon | last post by:
Hi, I am trying to learn C from scratch and, though I do know how to program in Python, many things in C are hard to understand - even after reading the examples. I guess because so many...
3
by: Vivienne | last post by:
I am using VS 2005. In a project when I was trying to write some unsigned char into a file, I used fwrite() and fputc(). But I found whenever I tried to write 0x0a, fwrite() function automaticly...
1
by: rohit deshpande | last post by:
i am writing a program in c,c++....... I want to read one file and write its contents using fread and fwrite functions. the program i hv written has no errors. but output file is not same as that of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.