Solved

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

Posted on 2003-11-23
4
714 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now