PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > using fputcsv()
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
using fputcsv()

Réponse
 
LinkBack Outils de la discussion
Vieux 08/11/2007, 15h34   #1
shenoyvikram@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut using fputcsv()

New to PHP

I am trying to search through a csv file. when I match a string I want
to replace it using the fputcsv function. However, I always notice
there is an extra line inserted. I am using fseek to move file
pointer.
- does fputcsv replace or insert data into csv file?
Code below and example of problem also.
while( ($data = fgetcsv($fid, 1000, ",")) !== FALSE) {
if (strcmp ($data[0], $compare) == 0){
fseek ($fid,$offset_counter);
fputcsv($fid, $data1);
$offset_counter = ftell ($fid);
}

My starting file
11,11,11,11
22,22,22,22
33,33,33,33

The output I expect
11,11,11,11
33,22,22,22
33,33,33,33

The output I am getting
11,11,11,11
33,22,22,22

33,33,33,33

  Réponse avec citation
Vieux 08/11/2007, 20h22   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using fputcsv()

shenoyvikram@yahoo.com wrote:
> New to PHP
>
> I am trying to search through a csv file. when I match a string I want
> to replace it using the fputcsv function. However, I always notice
> there is an extra line inserted. I am using fseek to move file
> pointer.
> - does fputcsv replace or insert data into csv file?
> Code below and example of problem also.
> while( ($data = fgetcsv($fid, 1000, ",")) !== FALSE) {
> if (strcmp ($data[0], $compare) == 0){
> fseek ($fid,$offset_counter);
> fputcsv($fid, $data1);
> $offset_counter = ftell ($fid);
> }
>
> My starting file
> 11,11,11,11
> 22,22,22,22
> 33,33,33,33
>
> The output I expect
> 11,11,11,11
> 33,22,22,22
> 33,33,33,33
>
> The output I am getting
> 11,11,11,11
> 33,22,22,22
>
> 33,33,33,33
>
>


You can't safely insert data into the file like this. You'll run into
even more problems if the the new line is of different size than the old
one.

What you need to do is read the whole file in and write it back out.
There are a couple of ways to do this. If it's a small file, you can
just read everything into an array then write it all out. For larger
files, you can read them in one line at a time and write them out to a
temporary file. Then when you're done, delete the original file and
rename the temporary one.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h53.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09512 seconds with 10 queries