Recent

Author Topic: Write to TextFile on specific location  (Read 7608 times)

chim

  • New member
  • *
  • Posts: 8
Write to TextFile on specific location
« on: September 19, 2014, 01:02:21 pm »
Hello pros!

could you please help me?

I just want to write in a Textfile some Text, but this in the specific location, and i always get with the seek command an error back.

This is my short Code:

var
  F: TextFile;
begin
   AssignFile(F, 'C:\test\neu.html');
   Append(F);
   FileSeek(F,175);
   Writeln(F, 'Right Place');
   Writeln(F, 'dont overwrite');
   CloseFile(F);
end;

without the Seek command it works fine, but with the command i get this error back:

unit1.pas(102,10) Error: Call by var for arg no. 1 has to match exactly: Got "Text" expected "File"

So i understand what it say to me, but i couldn't figure out how to solve this, every other command don't have a problem with this,
could you please help me pro's?

thx in advance :)
« Last Edit: September 19, 2014, 01:20:23 pm by chim »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Write to TextFile on specific place
« Reply #1 on: September 19, 2014, 01:19:50 pm »
The error is correct. Textfiles are not seekable.

chim

  • New member
  • *
  • Posts: 8
Re: Write to TextFile on specific location
« Reply #2 on: September 19, 2014, 01:22:50 pm »
thx for ur reply,

can i have a chance to get this problem solved, with some other commands?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Write to TextFile on specific location
« Reply #3 on: September 19, 2014, 03:41:47 pm »
Use a TStringList to load the test (LoadFromFile).
Find the location you need, insert the text and write back to file (SaveToFile)

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Write to TextFile on specific location
« Reply #4 on: September 19, 2014, 08:50:42 pm »
You can force textfiles to be seekable with the following routine. I don't recommend it (because it hacks the normally unseen TTextRec file metadata), but it seems to work, at least on Windows. I have not tested on other platforms.

Code: [Select]
function TextFileSeek(var F: TextFile; bytePos: integer): boolean;
const
  ErrorPos = $FFFFFFFF;
var
  FPointerPos: DWORD;
begin
  Result:=False;
  case TTextRec(F).Mode of
    fmInput:  begin
                FPointerPos:=SetFilePointer(TTextRec(F).Handle, bytePos, nil, FILE_BEGIN);
                if (FPointerPos = ErrorPos) then
                  SysErrorMessageUTF8(GetLastError)
                else Result:=True;
                TTextRec(F).bufend:=0;
              end;
    fmOutput: begin
                FPointerPos:=SetFilePointer(TTextRec(F).Handle, bytePos, nil, FILE_BEGIN);
                if (FPointerPos = ErrorPos) then
                  SysErrorMessageUTF8(GetLastError)
                else Result:=True;
                TTextRec(F).bufpos:=0;
              end;
    else
      SysErrorMessageUTF8(ERROR_FILE_NOT_FOUND);
  end;
end;

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Write to TextFile on specific location
« Reply #5 on: September 19, 2014, 10:19:02 pm »
You can force textfiles to be seekable with the following routine. I don't recommend it (because it hacks the normally unseen TTextRec file metadata), but it seems to work, at least on Windows. I have not tested on other platforms....
Still, it would be much easier for him to insert text in between by using TStringList. There is no method that pushes next bytes forward by written amount.

chim

  • New member
  • *
  • Posts: 8
Re: Write to TextFile on specific location
« Reply #6 on: September 22, 2014, 05:15:09 pm »
Thank you Guys for pointing me in the right direction.

I use now the LoadfromFile Method.

everything works well.

chim

  • New member
  • *
  • Posts: 8
Re: Write to TextFile on specific location
« Reply #7 on: September 24, 2014, 11:05:31 am »
Hello Pros,

could you please help me once again?  :-[

My code work for me just fine for one file, my problem is to get this actions to every file in my Memo3.

To make this clear what my App does:

I got a HTML Template, and i would change seperate parts of the file with new ones, and save this as a new file.

1) Choose Template
2) Template get loaded into Memo1
3) Pressing the Button will search and list every HTML File from the dir you choose into Memo3
4) Load into Memo4 the current file listed at first in Memo3
5) Load HTML Code from prelisted locations of Memo4 and write it to Memo2
6) Write the HTML Code from Memo2 into Memo1 to the prelistet locations
7) Save File and repeat the Step 4) to Step 7) until every file from Memo3 was done

So thats my plan, unfortunately it does this actions just one time, and i can't figure out why  :(

Could you please help me again?

here is the code:

Yes i know its a mess  :-[



Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
var
  i: integer;
  Ini: TIniFile;
  Pfad: String;
  Pfad2: Integer;
  Save: String;
  begin
    If comboBox1.Text ='Vorlage wählen...' then
begin
showmessage('Bitte Vorlage wählen!');
end
else
If comboBox1.Text ='Normalglas' then
begin
  if SelectDirectoryDialog1.Execute then
       begin
       GetFilesInDirectory(SelectDirectoryDialog1.FileName, '*.html', Memo3.Lines, True, True);
begin
for i:=0 to Memo3.Lines.Count-1 do
  Ini:=TIniFile.Create(GetApplicationDirectory+'Vorlagen.ini');
  try
     Pfad := Ini.ReadString('Normalglas', 'Pfad', '');
     Memo1.Lines.LoadFromFile(GetApplicationDirectory+Pfad);
  finally
    Ini.Free;
       begin
          Memo4.Lines.LoadFromFile(Memo3.Lines[i]);
          Memo2.Lines.Add(String(Memo4.Lines[12]));
          Memo2.Lines.Add(String(Memo4.Lines[14]));
          Memo2.Lines.Add(String(Memo4.Lines[15]));
          Memo2.Lines.Add(String(Memo4.Lines[38]));
          Memo2.Lines.Add(String(Memo4.Lines[50]));
          Memo2.Lines.Add(String(Memo4.Lines[55]));
          with ProgressBar1 do
       begin
       Ini:=TIniFile.Create(GetApplicationDirectory+'Vorlagen.ini');
       try
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter1', 10);
       Memo1.Lines[Pfad2]:= Memo2.Lines[0];
       ProgressBar1.Position := 15;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter2', 12);
       Memo1.Lines[Pfad2]:= Memo2.Lines[1];
       ProgressBar1.Position := 30;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter3', 13);
       Memo1.Lines[Pfad2]:= Memo2.Lines[2];
       ProgressBar1.Position := 45;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter4', 36);
       Memo1.Lines[Pfad2]:= Memo2.Lines[3];
       ProgressBar1.Position := 60;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter5', 48);
       Memo1.Lines[Pfad2]:= Memo2.Lines[4];
       ProgressBar1.Position := 75;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter6', 53);
       Memo1.Lines[Pfad2]:= Memo2.Lines[5];
       ProgressBar1.Position := 90;
         finally
         Ini.Free;
         Save:=(Memo3.Lines[0]+'glas.html');
           Memo1.Lines.SaveToFile(Save);
           ProgressBar1.Position := 100;
end;
end;
end;
end;
  end;
  end;
  end;
  end;

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Write to TextFile on specific location
« Reply #8 on: September 24, 2014, 11:17:59 am »
Could you please start a new thread for new questions? (Just copying over the contents of your previous post is fine ;) )

It cuts down on confusion, helps other when searching the forums for problems and solutions etc.

Danke!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

chim

  • New member
  • *
  • Posts: 8
Re: Write to TextFile on specific location
« Reply #9 on: September 24, 2014, 11:19:21 am »
All right Thank you

 

TinyPortal © 2005-2018