Page 1 of 1
readinf specific line of the file? how to?
Posted: Tue Nov 07, 2006 8:19 pm
by alexus
Hi,
Ok if i have opened the text file that has lines of data in it I want to search line by line if i have line let say containing "hello world" if I do hahe such line I want to be able to delte that line.
How can I do that?
Thanks for help!
Posted: Tue Nov 07, 2006 8:24 pm
by feyd
fopen() +
fread() or
fgets() + some comparison +
fclose().
Posted: Tue Nov 07, 2006 8:43 pm
by alexus
But none of those are reading file lines they read bytes, ... I can try to get contect of the file and the do foreach (hmm thats a VB) well separate the string by `/n` and then look (some how ) if one of the parts have my "hellow word" if yes then remove that array alemnt (some how) and write new srting back o file...
Hmm... i have no idea what im doing today... swithcing languages does hurt.
Posted: Tue Nov 07, 2006 8:48 pm
by feyd
You need to read a little closer:
fgets() wrote:Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first).
Posted: Tue Nov 07, 2006 8:59 pm
by alexus
yes i saw that, still dont see any wat to get lines... like for example if i do bash script for linux i would yse grep to get the line wthat contain my "Hello World". Basicaly if i have whis line "abc Hello Worl abc" and i do grep 'Helo World' I would get returned the full line... thats what im looking for
Posted: Tue Nov 07, 2006 9:03 pm
by feyd
fgets() fetches lines.. your comparison tells you whether the line contains your search term or not. That the same thing grep does.
Posted: Tue Nov 07, 2006 9:11 pm
by alexus
i dont see how to do this from the man, all sample code do something else
Posted: Tue Nov 07, 2006 9:23 pm
by feyd
What code have you tried?
Posted: Tue Nov 07, 2006 9:40 pm
by alexus
none, i just looked at it, i dont know where to start.
Posted: Tue Nov 07, 2006 9:47 pm
by John Cartwright
If the file you are loading is relatively small, I would use file() to load it into an array and loop using a foreach(), checking your match using if() and substr()
If the file is large use feyd's suggestion, and apply that comparison the same way.
As for feyd's suggestion, what are you having problems with? There are plenty examples for looping through file contents.