Board index » delphi » Reading and Writing

Reading and Writing

Couple of quick questions

Q1. I need to read a line of text, and then be able to write a line of text
back to the file, to the position after the rest of the text. So I need to
know how to tell the program that I want to write a line of text to where it
read upto last time.

Q2. How do I search for a string in a text file, for example... in a .txt
document or similar, how would I find the word "(Unique Word)" , and then
replace this word with some other word ?

Thanks in advance

Nick

 

Re:Reading and Writing


The best way to handle regular text file (.TXT) is to create a TMemo object in
memory, the use the LoadFromFile method to load all the text from the file into
memory.

When the text is in memory, you can access it easly usign the Text property, or
the Lines property.

To add a line use Lines.Add. To insert one use Lines.Insert... etc...

Search and Replace can also be done something like this:

procedure TForm1.FindDialog1Find(Sender: TObject);
{Finds some text from the cursor, you'll need to return the cursor to
?the beginning once you've gone through the file}
var
? StartPos: Integer;
? Buffer: PChar;
? SearchText: array[0..255] of Char;
? ThePos: PChar;
begin
? {Get the to-be-found text and make it uppercase as we are doing an 'ignore
case' search}
? StrPCopy(SearchText, FindDialog1.FindText);
? StrUpper(SearchText);
? {Get the starting position for the search}
? StartPos := Memo1.SelStart + 1;
? {Create a buffer to store the complete memo}
? GetMem(Buffer, Memo1.GetTextLen + 1);
? try
???? {Get the contents of the memo and convert to uppercase}
???? Memo1.GetTextBuf(Buffer, Memo1.GetTextLen + 1);
???? StrUpper(Buffer);
???? {Find the text, starting at the current position + 1}
???? ThePos := StrPos(@Buffer[StartPos], SearchText);
???? if ThePos <> nil then
???? begin
??????? {If found, calculate the new position of the cursor and selected text}
??????? Memo1.SetFocus;
??????? Memo1.SelStart := StartPos + (ThePos - @Buffer[StartPos]);
??????? Memo1.SelLength := Length(FindDialog1.FindText);
???? end
???? else
??????? MessageBeep(0);
? finally
???? FreeMem(Buffer, Memo1.GetTextLen + 1);
? end;
end;

Erez Steinberg,
ere...@ibm.net

Quote
Nick wrote:
> Couple of quick questions

> Q1. I need to read a line of text, and then be able to write a line of text
> back to the file, to the position after the rest of the text. So I need to
> know how to tell the program that I want to write a line of text to where it
> read upto last time.

> Q2. How do I search for a string in a text file, for example... in a .txt
> document or similar, how would I find the word "(Unique Word)" , and then
> replace this word with some other word ?

> Thanks in advance

> Nick

Re:Reading and Writing


I know what your trying to do is not very easy to do this using text files..
 if you used untyped files you could then use the Seek to position the file
pointer
to read and write, how ever, you then must process the <LF>and <CR> of
each line which is part of the file..
 values #10 and #13.
--
 the simplest way to do this is to load up a Tstring  , this object has a
loadfromfile
from there you can work with separate lines ect..
Quote
Nick wrote:
> Couple of quick questions

> Q1. I need to read a line of text, and then be able to write a line of text
> back to the file, to the position after the rest of the text. So I need to
> know how to tell the program that I want to write a line of text to where it
> read upto last time.

> Q2. How do I search for a string in a text file, for example... in a .txt
> document or similar, how would I find the word "(Unique Word)" , and then
> replace this word with some other word ?

> Thanks in advance

> Nick

Re:Reading and Writing


All solve i see don't use the simply text file function...
So here they are
File : Text;
Readln, writeln, append, reset, rewrite.... check all this functions (append is
what you need)
To read/write a specific line number you must read from start of file and write
to another file (text file limitation)

And to search/replace see this sample (search 'simple search' and replace with
'hohoho')
var test: string
filer, fileo: text;

reset(filer);
rewrite(fileo);
repeat
readln(filer,test);
if test='simple search' then writeln(fileo,'hohoho')
                                 else writeln(fileo,test);
until eof(filer);

Quote
Nick wrote:
> Couple of quick questions

> Q1. I need to read a line of text, and then be able to write a line of text
> back to the file, to the position after the rest of the text. So I need to
> know how to tell the program that I want to write a line of text to where it
> read upto last time.

> Q2. How do I search for a string in a text file, for example... in a .txt
> document or similar, how would I find the word "(Unique Word)" , and then
> replace this word with some other word ?

> Thanks in advance

> Nick

Re:Reading and Writing


In answer to your second question about searching for a word then
replacing it. try this nifty little function

Function Alter(Line_of_Text,Search,Replace:String):String;
Var A:Byte;
Begin
A:=Pos(Search,Line_of_Text);
If A>0 then Begin
Delete(Line_of_Text,A,length(Search));Insert(Line_of_Text,Replace,A);
End;
Alter:=Line_of_Text;
End;

Procedure Main_Program;
Var T:Textfile;s:string;
Begin
Assignfile(T,'Yourfile.TXT');reset(t);
repeat
readln(t,s);
S:=Alter(S,'My Word','Your Word');
listbox1.items.add(s);
Until eof(t);
closefile(t);
end.

On Fri, 20 Aug 1999 15:18:20 +0300, Erez Steinberg <ere...@ibm.net>
wrote:

Quote
>The best way to handle regular text file (.TXT) is to create a TMemo object in
>memory, the use the LoadFromFile method to load all the text from the file into
>memory.

>When the text is in memory, you can access it easly usign the Text property, or
>the Lines property.

>To add a line use Lines.Add. To insert one use Lines.Insert... etc...

>Search and Replace can also be done something like this:

>procedure TForm1.FindDialog1Find(Sender: TObject);
>{Finds some text from the cursor, you'll need to return the cursor to
>?the beginning once you've gone through the file}
>var
>? StartPos: Integer;
>? Buffer: PChar;
>? SearchText: array[0..255] of Char;
>? ThePos: PChar;
>begin
>? {Get the to-be-found text and make it uppercase as we are doing an 'ignore
>case' search}
>? StrPCopy(SearchText, FindDialog1.FindText);
>? StrUpper(SearchText);
>? {Get the starting position for the search}
>? StartPos := Memo1.SelStart + 1;
>? {Create a buffer to store the complete memo}
>? GetMem(Buffer, Memo1.GetTextLen + 1);
>? try
>???? {Get the contents of the memo and convert to uppercase}
>???? Memo1.GetTextBuf(Buffer, Memo1.GetTextLen + 1);
>???? StrUpper(Buffer);
>???? {Find the text, starting at the current position + 1}
>???? ThePos := StrPos(@Buffer[StartPos], SearchText);
>???? if ThePos <> nil then
>???? begin
>??????? {If found, calculate the new position of the cursor and selected text}
>??????? Memo1.SetFocus;
>??????? Memo1.SelStart := StartPos + (ThePos - @Buffer[StartPos]);
>??????? Memo1.SelLength := Length(FindDialog1.FindText);
>???? end
>???? else
>??????? MessageBeep(0);
>? finally
>???? FreeMem(Buffer, Memo1.GetTextLen + 1);
>? end;
>end;

>Erez Steinberg,
>ere...@ibm.net

>Nick wrote:

>> Couple of quick questions

>> Q1. I need to read a line of text, and then be able to write a line of text
>> back to the file, to the position after the rest of the text. So I need to
>> know how to tell the program that I want to write a line of text to where it
>> read upto last time.

>> Q2. How do I search for a string in a text file, for example... in a .txt
>> document or similar, how would I find the word "(Unique Word)" , and then
>> replace this word with some other word ?

>> Thanks in advance

>> Nick

Re:Reading and Writing


 Q1

assignfile(outputfile,
append(outputfile);// add line to bottom of file
 writeln(outputfile,

assignfile(command_file,
 rewrite(command_file);//over type existing text
 writeln(command_file

AssignFile
Readln(  //reads line and moves to next line ready for next read

if you wnat to go back to the exact same line just remebr how nmany reads or
writes you done and repeat

Quote
Nick <mrnic...@hotmail.com> wrote in message

news:7pi8f9$ut6$1@news8.svr.pol.co.uk...
Quote
> Couple of quick questions

> Q1. I need to read a line of text, and then be able to write a line of
text
> back to the file, to the position after the rest of the text. So I need to
> know how to tell the program that I want to write a line of text to where
it
> read upto last time.

> Q2. How do I search for a string in a text file, for example... in a .txt
> document or similar, how would I find the word "(Unique Word)" , and then
> replace this word with some other word ?

> Thanks in advance

> Nick

Other Threads