Solved

help! WriteLN  to write to a specific line in text file

Posted on 2003-11-23
4
722 Views
Last Modified: 2010-04-03
hi

I am trying to write a program with a checkbox, so that when the checkbox is clicked the following is written to a text file

Dave speaks good english true;

if the checkbox is unclicked

Dave speaks good english false;

When using WriteLn it writes to the first line of the text file i have specified, but i want to write to another line, for example, line 10. I have the following code which i couldnt get to work.


procedure Tform1.CheckBox1Click(Sender: TObject);

var
txt:textfile;
line:string;

begin
assignfile(txt, '/home/user/test.txt');
rewrite(txt);

if checkbox1.checked then

line:='dave speaks good english true;';
Writeln(txt, line)

end

else

line :='dave speaks good english false;';
Writeln(txt,line);
end

closefile(txt);
end;
end.

My first problem is that i cant seem to use the if else statement to write a line to a text file if the checkbox is clicked if not clicked write a different line to a text file, and the second problem is that i dont know how i could change my code to write to another line of the text file rather then line 1, for example line 10.   Thanks in advance!!

0
Comment
Question by:itektas
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:gmayo
ID: 9805874
You need begin...end blocks in your IF statement:

if checkbox1.checked then begin

line:='dave speaks good english true;';
Writeln(txt, line)

end

else begin

line :='dave speaks good english false;';
Writeln(txt,line);
end

Geoff M.
0
 

Author Comment

by:itektas
ID: 9805888
ok cool ill try that now, but what about if i want to enter the information in a certain line other then the first one, Writeln writes into the first line of the text file, how would i write to line 10 ? is that possible? thanks
0
 
LVL 8

Accepted Solution

by:
gmayo earned 30 total points
ID: 9806007
I think you can open the file in read/write mode, read 9 lines, and write the 10th. However, if the new line is shorter, I think you'll have part of the old line left over.

Perhaps a better option would be to use a TStringList.

var sl : TStringList;

sl := TStringList.Create;
sl.LoadFromFile('filename.txt');
sl[9] := 'whatever you want';
sl.SaveToFile('filename.txt');
sl.Free;

Geoff M.
0
 

Author Comment

by:itektas
ID: 9806179
perfect. thanks a lot :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question