Strange character appear when i truncate a text file

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Americanhorizon
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Strange character appear when i truncate a text file

Postby Americanhorizon » Wed Nov 14, 2012 4:51 am

Hi, when i attempo to truncate a file to 0 and write some text inside it, i've a strange problem

This is the code

$errors=fopen("error_links.txt", "r+");
$leggiErr=fread($errors, filesize("error_links.txt"));
$videoArray = explode("\n", $leggiErr);

ftruncate($errors,0);
fwrite($errors, "lol");


And this is the result:
������������������������������������������������������������������lol


What are this series of "�" ?? Note that i've deleted a lot of it, they are much more!

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Strange character appear when i truncate a text file

Postby seandisanti » Wed Nov 14, 2012 9:44 am

why truncate? just open for r instead of r+, do what you need to, then close file and reopen with w and write what you want.

Americanhorizon
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Re: Strange character appear when i truncate a text file

Postby Americanhorizon » Wed Nov 14, 2012 9:51 am

i want to open file and delete all his contents

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Strange character appear when i truncate a text file

Postby seandisanti » Wed Nov 14, 2012 10:11 am

which

Code: Select all

$new_erros=fopen("error_links.txt", "w");

accomplishes

Americanhorizon
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Re: Strange character appear when i truncate a text file

Postby Americanhorizon » Wed Nov 14, 2012 10:34 am

i will do this thanks,
but would be useful in future know why the first method generate all those strange character

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Strange character appear when i truncate a text file

Postby seandisanti » Wed Nov 14, 2012 10:50 am

I am sure that reading through the comments on php.net/fopen may shed more light on the problem, but since there was a working solution i didn't bother trying too hard to recreate failure conditions, sorry.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests