Page 1 of 1
txt file (lil help?)
Posted: Tue Dec 28, 2004 6:08 pm
by shiznatix
ok i am editing a text file line by line, i have it so i can view each line in its own text field with a submit button for each line and all that happy stuff BUT! i don't know how to open a txt file, automatically go to a specific line, then only write to that one line without changing anything else.

Posted: Tue Dec 28, 2004 6:15 pm
by timvw
that is more or less impossible. you will have to read the whole file, and then remove/edit that those parts and write the new contents of the file again...
in the case the new string has exactly the same length, you could also position your filepointer with fseek and then fwrite from there...
but as you say you are offering all the lines in separate inputboxes to the user, you could also split your big text file in separate textfiles... this way, you only have to edit the file that is associated to that textbox.
fe: file1.txt with 10 lines would become file1-1.txt, file1-2.txt, ... , file1-10.txt
Posted: Tue Dec 28, 2004 6:38 pm
by shiznatix
alright well nothing is impossible (well i hope THIS isnt impossible because i will be SOL if it is) but is there a way to calculate the amount of bytes up to a certain line so i can set the file pointer to the line i want with fseek? or do i not know what im talking about at all?
timvw wrote:fe: file1.txt with 10 lines would become file1-1.txt, file1-2.txt, ... , file1-10.txt
that way seams a little...sloppy...any other sugestions?
Posted: Tue Dec 28, 2004 7:38 pm
by m3rajk
give us a little background on what you're trying.... right now i think sql would be far more efficient, and if it is, i'd just rather point you in that dir, but to really eval, i'd need more info about what you're doing
Posted: Tue Dec 28, 2004 8:09 pm
by shiznatix
ok well yes a database mite be easier or somtin maybe but i am using flat txt files because:
1) i need to learn them sooner or later
2) my father died in a horrible train crash involving nothing but sql
what im doing is making a news script and for every news topic thing added a txt file is made to for the comments section so all comments that are posted are saved to that news topics specific txt file. so in the admin page i want to be able to edit comments made by other users. i have this for the showing of the individual lines for editing
Code: Select all
<?php
$txt = "comments/$file";
$opentxt = fopen($txt, 'r');
?><form action="admin.php?id=editcomments2&file=<? print $file; ?>" method="post"><?
$i = 1;
while (!feof($opentxt)) {
$readtxt = fgetss($opentxt, 5000);
?><input type="text" name="editedcom" value="<? print $readtxt; ?>">
<input type="submit" name="submit" value="Edit Text">
<input type="hidden" name="linenum" value="<? print $i; $i++; ?>">
</form>
<br>
<?
}
fclose($opentxt);
?>
but how do i make it so when i hit the individual submit button it replaces the line with the number of linenum...u know what i mean(i hope)
Posted: Tue Dec 28, 2004 8:44 pm
by feyd
there's a potential to use [php_man]file()[/php_man] to load in all the lines, swap the correct line, then write out the file again.. but as tim and m3rajk have said, a database is probably the better direction.
Posted: Tue Dec 28, 2004 8:48 pm
by shiznatix
that what i thought but im not sure how to go about doing that...(not the database thing the file thing) i do completly understand that a dabase is better than txt files but i need to use a txt file here
Posted: Tue Dec 28, 2004 9:01 pm
by feyd
well.. the file thing involves [php_man]file()[/php_man], [php_man]array[/php_man] accesses, [php_man]fopen()[/php_man], and [php_man]fwrite()[/php_man].
Posted: Tue Dec 28, 2004 9:28 pm
by shiznatix
hummm arrays yes? well iv never really done things with them and i truley dont much understand them but WTF i gotta learn em somtime! i best rtfm, ill post if i need any more help or if i get a solution ill post that
Posted: Wed Dec 29, 2004 2:08 am
by carlmcdade
I always point to this tutorial becasue the comments are so that they make it hard not to understand
http://codewalkers.com/tutorials/57/9.html
Posted: Wed Dec 29, 2004 4:17 pm
by shiznatix
wowzaz thats bitchin good! thanks much you swedish love machine
