473,418 Members | 2,267 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,418 software developers and data experts.

File reading and deleting a pattern (beginner)

Dear group,
I am a beginner in php and I was little bit experience in C language.
I
want to read a file's content and delete the first line of the file and

display those lines which has got deleted.

The content of the external file , which is in .cvs format is:

-------------------------------------------------------------------
Agency,Ebook,Date,Sold#,Sold$,Refund#,Refund$,GAW# ,GAW$,Income
cbezy,Dog,8/7/06,0,0,0,0,204,16.88,-16.88
cbezy,CMS,8/7/06,1,10.79,0,0,0,0,10.79
cbgeo,Sudoku,8/7/06,1,14.72,0,0,0,0,14.72
cbgeo,Cock,8/7/06,1,14.72,0,0,0,0,14.72
ppgeo,Sudoku,8/7/06,17,229.58,2,24.16,2286,118.62,86.8
ppgeo,Pythons,8/7/06,1,19.36,0,0,250,8.84,10.52
ppgeo,Cockatiels,8/7/06,9,108.46,1,16.21,0,0,92.25
ppgeo,Cockatoos,8/7/06,5,72.66,0,0,743,47.56,25.1
ppgeo,Wastewater,8/7/06,1,44.87,0,0,49,5.25,39.62
ppgeo,Chinchillas,8/7/06,1,6.91,0,0,0,0,6.91
ppgeo,Modhomes,8/7/06,0,0,0,0,189,99.94,-99.94
ppgeo,Smormonsub,8/7/06,1,3.54,0,0,0,0,3.54
--------------------------------------------------------------------

The small program which I wrote is

<?php
function read_file($fname)
{
$f = fopen($fname,"r");
if(!$f)
die("file does not exist");
$buff = fread($f,filesize($fname));
if(!$buff)
die("file reading failed");
return $buff;
}

function parse_condent($buff,$fname)
{
$f = fopen($fname, "a+");
if(!$f)
die("file does not exist");
if($buff == '\n' || $buff == '\r' || $buff == '\t') /*####*/
unset($buff);

return $buff;
}
$f = read_file("1.csv");

echo $f.'<brbefore delition <br>';

$p = parse_condent($f,"1.csv");

echo $p.'<brAfter deletion';
?>

when the line marked with /*####*/ is taken then the whole array is
getting
deleted. I want the specified line and chars. Any help or url link is
appreciated.

<OT>
In C programming the string, which is a pointer is iterated and
checked with
a specified pattern. How is that possible in php?
</OT>

Aug 10 '06 #1
2 1818
fool wrote:
Dear group,
I am a beginner in php and I was little bit experience in C language.
I
want to read a file's content and delete the first line of the file and

display those lines which has got deleted.

The content of the external file , which is in .cvs format is:

-------------------------------------------------------------------
Agency,Ebook,Date,Sold#,Sold$,Refund#,Refund$,GAW# ,GAW$,Income
cbezy,Dog,8/7/06,0,0,0,0,204,16.88,-16.88
cbezy,CMS,8/7/06,1,10.79,0,0,0,0,10.79
cbgeo,Sudoku,8/7/06,1,14.72,0,0,0,0,14.72
cbgeo,Cock,8/7/06,1,14.72,0,0,0,0,14.72
ppgeo,Sudoku,8/7/06,17,229.58,2,24.16,2286,118.62,86.8
ppgeo,Pythons,8/7/06,1,19.36,0,0,250,8.84,10.52
ppgeo,Cockatiels,8/7/06,9,108.46,1,16.21,0,0,92.25
ppgeo,Cockatoos,8/7/06,5,72.66,0,0,743,47.56,25.1
ppgeo,Wastewater,8/7/06,1,44.87,0,0,49,5.25,39.62
ppgeo,Chinchillas,8/7/06,1,6.91,0,0,0,0,6.91
ppgeo,Modhomes,8/7/06,0,0,0,0,189,99.94,-99.94
ppgeo,Smormonsub,8/7/06,1,3.54,0,0,0,0,3.54
--------------------------------------------------------------------

The small program which I wrote is

<?php
function read_file($fname)
{
$f = fopen($fname,"r");
if(!$f)
die("file does not exist");
$buff = fread($f,filesize($fname));
if(!$buff)
die("file reading failed");
return $buff;
}

function parse_condent($buff,$fname)
{
$f = fopen($fname, "a+");
if(!$f)
die("file does not exist");
if($buff == '\n' || $buff == '\r' || $buff == '\t') /*####*/
unset($buff);

return $buff;
}
$f = read_file("1.csv");

echo $f.'<brbefore delition <br>';

$p = parse_condent($f,"1.csv");

echo $p.'<brAfter deletion';
?>

when the line marked with /*####*/ is taken then the whole array is
getting
deleted. I want the specified line and chars. Any help or url link is
appreciated.

<OT>
In C programming the string, which is a pointer is iterated and
checked with
a specified pattern. How is that possible in php?
</OT>
Hi there,

humm so you want the first line back right? The first line is the one
you deleted? If thats the case below is the modifucations to your code.
Also buff gets reset once it hits unset() so it returns nothing.

<?php
function read_file($fname)
{
$f = fopen($fname,"r");
if(!$f)
die("file does not exist");
$buff = fread($f,filesize($fname));
if(!$buff)
die("file reading failed");
return $buff;
}

function parse_condent($buff,$fname)
{
$f = fopen($fname, "a+");
if(!$f)
die("file does not exist");

$content = fread($f, 20000);
$lines = explode("\n", $content);

//remoevs the first line
$lines = array_splice($lines, 0, -1);

return $lines[0];
}
$f = read_file("test.csv");

echo $f.'<brbefore delition <br><br>';

$p = parse_condent($f,"test.csv");

echo $p.'<brAfter deletion';
?>

Aug 11 '06 #2
In article <9q*****************@newssvr14.news.prodigy.com> ,
ma*****@prodigy.net says...
fool wrote:
Dear group,
I am a beginner in php and I was little bit experience in C language.
I
want to read a file's content and delete the first line of the file and

display those lines which has got deleted.

The content of the external file , which is in .cvs format is:

-------------------------------------------------------------------
Agency,Ebook,Date,Sold#,Sold$,Refund#,Refund$,GAW# ,GAW$,Income
cbezy,Dog,8/7/06,0,0,0,0,204,16.88,-16.88
cbezy,CMS,8/7/06,1,10.79,0,0,0,0,10.79
cbgeo,Sudoku,8/7/06,1,14.72,0,0,0,0,14.72
cbgeo,Cock,8/7/06,1,14.72,0,0,0,0,14.72
ppgeo,Sudoku,8/7/06,17,229.58,2,24.16,2286,118.62,86.8
ppgeo,Pythons,8/7/06,1,19.36,0,0,250,8.84,10.52
ppgeo,Cockatiels,8/7/06,9,108.46,1,16.21,0,0,92.25
ppgeo,Cockatoos,8/7/06,5,72.66,0,0,743,47.56,25.1
ppgeo,Wastewater,8/7/06,1,44.87,0,0,49,5.25,39.62
ppgeo,Chinchillas,8/7/06,1,6.91,0,0,0,0,6.91
ppgeo,Modhomes,8/7/06,0,0,0,0,189,99.94,-99.94
ppgeo,Smormonsub,8/7/06,1,3.54,0,0,0,0,3.54
--------------------------------------------------------------------

The small program which I wrote is

<?php
function read_file($fname)
{
$f = fopen($fname,"r");
if(!$f)
die("file does not exist");
$buff = fread($f,filesize($fname));
if(!$buff)
die("file reading failed");
return $buff;
}

function parse_condent($buff,$fname)
{
$f = fopen($fname, "a+");
if(!$f)
die("file does not exist");
if($buff == '\n' || $buff == '\r' || $buff == '\t') /*####*/
unset($buff);

return $buff;
}
Thanks for the code. I have learnt somthing.
Aug 11 '06 #3

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

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
7
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in...
23
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use...
1
by: Wayne Aprato | last post by:
Is there an effective method of preventing users from accidentally or maliciously deleting the database file/files from a shared network drive? At the moment, I'm using a batch file to copy the...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
8
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some...
10
by: kriz4321 | last post by:
I have many files in a dirctory in which I need to make the common subsitution. I need to delete all lines between two matched patterns I need to match a line having words " chkstats to...
5
by: pramodkh | last post by:
Hi All I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line. for example, I have the following content in a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.