473,383 Members | 1,840 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,383 software developers and data experts.

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

Nov 8 '07 #1
1 4077
sh**********@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.
js*******@attglobal.net
==================

Nov 8 '07 #2

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

Similar topics

2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
4
by: mpar612 | last post by:
Hello, I'm sort of new to PHP. I am using the following code to retrieve a column from a database and to put all of the results into a .csv file. This allows creates a .csv file, but only...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
by: shenoyvikram | last post by:
New to PHP I am running a loop searching a csv file. When I have a match I am trying to replace the matched line with new data. However everytime I do the replace, there is an empty line in the...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.