Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-23
4
Medium Priority
?
778 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 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

Independent Software Vendors: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses

927 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