Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

18 Experts available now in Live!

Get 1:1 Help Now