Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-23
4
Medium Priority
?
753 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 120 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

715 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