PDA

View Full Version : Editting Specific Line of File



DarknessX
07-05-2007, 01:45 AM
System: WinXP
Compiler/IDE: NFPC
Libraries/API: 'None'

Ok, well, I have a setup for adding timers to my app, to keep the program from thinking the file ended (it's the way it's setup, it's not using EOF so a blank line screws it up) by reading a file, and going until a line is empty (or, ''). I made a workaround, so the timer file remains, and so does the entry in the file, and the name which is re-read from within the timer file is switched to 'EMPTY' and EMPTY timers are skipped. HOWEVER, this doesn't work so well...

Now, what I need, is how do I make it overwrite a specific line? Cause heres how it works:
List.txt -->
-search list.txt, put contents of each line to timer[x], x being the # of the particular timer... line 1 is in timer[1] and etc, using a repeat.. until loop.

/timers/ -->
-each timer[x] is used to read from a file...
--assign(f,path+'/timers/'+timer[x]+'.tmr');
---When a timer file is deleted, it deletes the entire timer file.

So, when it calls the name of the timer from list.txt, it closes the app because of errors. How would I edit line 'x' to remove the current line, and back all lines ahead of it down by 1? (IE, don't leave a blank line)

Chebmaster
07-05-2007, 06:41 AM
TStringList.LoadFromFile
TStringList.Delete
TStringList.SaveToFile

are the most easy tools to do the job.

DarknessX
13-05-2007, 03:30 PM
Ok, so how would these be used? I'm not using OOP pascal, and I don't know OOP pascal... I'm in the process of learning it.

Edit: Well, I figured out a workaround.

procedure
textcolor(7);
Writeln('You wish to remove a timer.');
Writeln('A list of the currently made timers follows.');
Writeln('Make sure to remember the number of the timer, as that is how');
Writeln('you will identify the timer.');
Readln;
ListItems;
Readln;
repeat
ClrScr;
Writeln('So which timer do you wish to delete?');
Readln(y);
textcolor(12);
Writeln('Are you sure you want to delete Timer ',timer[y],'? Type ''y'' to confirm.');
Readln(confirms);
if confirms = 'y' then confirm := true;
until confirm = true;
confirm := false;
begin
//Delete timer file, then edit list.txt and remove item.
//Delete file goes here...

//Edit list.txt...
x := 1; //First timer first :)
z := 1;
assign(f,'list.txt'); //open the file with the timers in it
rewrite(f);
repeat
timer[x] := timer[z]; //timer[#] becomes equal to timer[#b].
if y <> 1 then writeln(f,timer[x]); //If deleting timer #1, it would not delete without this line.
if x <y> y then begin x := x+1; z := z+1; end; //if x greater than y, go up one timer.
if x = y then begin y := 0; z := z+1; end; {if x = y, then increase #b, but not #a. Effect: when everything is written to at the beginning, the timer immediately above the current (or, x) is assigned to the current, therefor deleting a line out of the file.}
until timer[x] = ''; {until last timer.}

close(f);
end;
textcolor(7);
Exit;
end;

M109uk
13-05-2007, 03:38 PM
You could use it like:



var
strings&#58; TStringList;
begin
strings &#58;= TStringList.Create;
strings.LoadFromFile&#40;'timer.txt'&#41;;

// to change a value at index 0
strings&#91;0&#93; &#58;= 'new value';

// to delete a value at index 0
strings.Delete&#40;0&#41;;

strings.SaveToFile&#40;'timer.txt'&#41;;
strings.Free;
end;

DarknessX
13-05-2007, 05:23 PM
Hmmm, well.. I guess I rushed too much :P I was working on another alternative while I waited for a reply and I got it working perfectly just when you replied. I'd use the new way (it seems much easier) but it would require converting my save files over... Not difficult, but theres no point now.

Thanks for the help, cause I'll probably use it in a future program.