text (textfiles) type lazarus / 61-Beginners ---------------------------------------------------------------------- nugax · 2022-03-18 15:50 If I am using textfiles type, how do I seek back to the begining. Seek(F, 0) doesnt work (although the docs says it should with this filetype). Should I use another data type that F: text or F:textfiles. The docs arent really that helpful because whats in there doesnt exactly work as written... I Am use TIniFile and trying to add '; comment' to a created inifile. So I wrote a routine to go to a line and write the commend after creating the inifile. There is no routine to add a comment, nor can you add a comment before completely finishing the write on the inifile as it doesnt write it to disk until the end (so I think). ---------------------------------------------------------------------- Thaddy · 2022-03-18 15:56 TextFiles is nonsense. The correct syntax is simply text. And the documentation is 100% correct (not 99.9). You should read about flush.... https://www.freepascal.org/docs- html/rtl/system/flush.html ---------------------------------------------------------------------- korba812 · 2022-03-18 16:18 Quote from: Thaddy on March 18, 2022, 04:56:59 pmTextFiles is nonsense.Maybe he meant "TextFile" - an alias of the "Text" type? ---------------------------------------------------------------------- nugax · 2022-03-18 16:19 Quote from: Thaddy on March 18, 2022, 04:56:59 pmTextFiles is nonsense. You are proably right. But here is the excerpt of the example from: https://wiki.freepascal.org/File_Handling_In_Pascal Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program CreateFile; uses Sysutils; const  C_FNAME = 'textfile.txt';  var  tfOut: TextFile;                              <-----------------------------------------  ??????????   begin  // Set the name of the file that will be created  AssignFile(tfOut, C_FNAME);   // Use exceptions to catch errors (this is the default so not absolutely requried)  {$I+}   // Embed the file creation in a try/except block to handle errors gracefully  try    // Create the file, write some text and close it.    rewrite(tfOut);     writeln(tfOut, 'Hello textfile!');    writeln(tfOut, 'The answer to life, the universe and everything: ', 42);     CloseFile(tfOut);   except    // If there was an error the reason can be found here    on E: EInOutError do      writeln('File handling error occurred. Details: ', E.ClassName, '/', E.Message);  end;   // Give feedback and wait for key press  writeln('File ', C_FNAME, ' created if all went ok. Press Enter to stop.');  readln;end. ---------------------------------------------------------------------- nugax · 2022-03-18 16:20 Quote from: korba812 on March 18, 2022, 05:18:45 pmQuote from: Thaddy on March 18, 2022, 04:56:59 pmTextFiles is nonsense.Maybe he meant "TextFile" - an alias of the "Text" type? it literally in the example on the wiki. ---------------------------------------------------------------------- nugax · 2022-03-18 16:23 Not sure how to make flush work since the inifile unit is all self contained, without rewriting the TInifile class. Im not writing the inifiles by text. Im using a built in unit. There is no "flush" member and it just creates the inifile after you close it. ---------------------------------------------------------------------- nugax · 2022-03-18 16:27 Quote from: Thaddy on March 18, 2022, 04:56:59 pmTextFiles is nonsense. The correct syntax is simply text. And the documentation is 100% correct (not 99.9). You should read about flush.... https://www.freepascal.org/docs- html/rtl/system/flush.html Also, in the docs, it shows seek(F, 0) should work. yet it doesnt work with the textfile (text) file type... But its under the list of functions that do work in: https://wiki.freepascal.org/File_Handling_In_Pascal So either the wiki is wrong, textfile datatype is not needed or I should be using something other than what the example shows, or something isnt right in my code (which I doubt because its like 3 lines AssignFile(), Append(), Seek()  and file exists. ---------------------------------------------------------------------- korba812 · 2022-03-18 16:30 The documentation says you cannot use the Seek function for a "text" file: https://www.freepascal.org/docs-html/rtl/system/seek.html ---------------------------------------------------------------------- nugax · 2022-03-18 16:31 exact error: cbbsinifile.pas(98,11) Error: Call by var for arg no. 1 has to match exactly: Got "Text" expected "File" Thats when using text or textfile var F: text or F: textfile seek(F, 0); ---------------------------------------------------------------------- MarkMLl · 2022-03-18 16:33 Close, reopen, and stop arguing. Text files can't be sought. MarkMLl ---------------------------------------------------------------------- nugax · 2022-03-18 16:35 Im not arguing. Or not trying to if perceived. Thanks for the help. ---------------------------------------------------------------------- Thaddy · 2022-03-18 16:50 Quote from: MarkMLl on March 18, 2022, 05:33:14 pmText files can't be sought. Yes they can... But it takes a bit of programming. Not very much. ---------------------------------------------------------------------- Josh · 2022-03-18 17:01 Hope this helps a file of type Text of TextFile is exactly that, your telling pascal the that file your assigning is purely text and has no structure, and as it has no fixed structure the seek command will fail. seek is used for a structure file type. ie TCustomer=record Name:String[80]; age:Int32; Address1,Address2,Address3:String[80]; end;  // remember to use explicit varaible type; ie type integer size will change between bitness of application; and your data files will not work be transferable across bitness/platforms The idea here is to create a file to access a file that contains the data above, the record structure is a fixed size of bytes 324 bytes (*give or take before some jumps in) per record and because its a fixed structure, you can then seek to a given record; but first the file must be opened with the correct command. ie var fn:File of TCustomer;      TempCustomerDetails:TCustomer; ... Assignfile(fn,'MYCUSTOMERS.DAT'); Seek(fn,5); Seek customer recordnumber 5 ( like all pascal its a zero based file, so record 0 is quite often used by programmers to hold specifics; defaults etc regarding the structure; just a hint); then you read(fn,TempCustomerDetails); will then read the record into the variable. remember after every file operation you should check for any errors with ioresult; and inclose the commands in a {$-} and {$+} so that you can catch the errors; ie {$I-} seek(fn,5); {$I+} the io result could return; file not open; seek past end of file etct etc. as none of the code above is compilable; I have lepft out code tags. ---------------------------------------------------------------------- MarkMLl · 2022-03-18 17:03 Quote from: Thaddy on March 18, 2022, 05:50:15 pmQuote from: MarkMLl on March 18, 2022, 05:33:14 pmText files can't be sought. Yes they can... But it takes a bit of programming. Not very much. OK, but specifically if you're talking about "Text" as a predefined Pascal type you've got to start getting into implementation-specific stuff which is generally not at all a good idea. I have in the past done some useful stuff which first built up an index of line starts and then multiple indexes based on the original file sorted on various fields... and while that was seekable it didn't attempt to use a Pascal-style "Text" file. Going back to OP: while .ini files /can/ have comments, I don't think FPC's implementation has the capability of inserting them... and in particular of inserting them at a specified location. If there is an inescapable requirement to do this, I suspect that the best thing to do would be to pull the entire file into a TStringList since this does, after all, already do much of the key=value work. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2022-03-18 17:06 Quote from: josh on March 18, 2022, 06:01:12 pmas none of the code above is compilable; I have lepft out code tags. Speaking personally, I for one would prefer it if you did use code tags since I don't like trying to read proportionally-spaced sourcecode. God created punched cards to be monospaced, and we should respect his design choices. MarkMLl ---------------------------------------------------------------------- BobDog · 2022-03-18 19:49 SEEK(0...) would probably overwrite the first few characters anyway (as in c) You can easily slog out a prepend and append routine. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  {$IOCHECKS OFF}usessysutils; function filelength(filename:ansistring):longword;Var F : File of byte;var L:longword;beginAssign (F,filename);  Reset (F);  L:=FileSize(F);  Close (F);  exit(L);end; procedure loadfile(var content: ansistring;filename:ansistring);   Var Fin : File;   Var x:longint;   begin   x:=filelength(filename);   setlength(content,x);   Assign (Fin,filename);   {$I-}    Reset (Fin,x);   {$I+}    if IOResult<>0 then   begin   Writeln ('Error opening file: ', filename);   exit   end;   BlockRead (Fin,content[1],1);   close(fin);end;  procedure savefile(s:ansistring ;filename:ansistring);    var    fout:file;    begin    Assign(fout,filename);    Rewrite(fout,length(s));    blockwrite(fout,s[1],1);    close(fout);  end;    procedure prepend(content:ansistring;filename:ansistring);  var  s,L:ansistring;  begin  loadfile(L,filename);  L:=content+L;  savefile(L,filename);  end;   procedure append(content:ansistring;filename:ansistring);  var  s,L:ansistring;  begin  loadfile(L,filename);  L:=L+content;  savefile(L,filename);  end;  vars:ansistring='abcdefghijklmnopqrstuvwxyz'; ans:ansistring;fna me:ansistring='seektest.txt';  begin savefile(s,fname); loadfile(ans,' seektest.txt'); writeln(ans); prepend('Comment: ',fname); loadfile(ans,'seektest.txt'); writeln(ans); append(' (please check if the file has been deleted (below : FALSE =OK))',fname);  load file(ans,'seektest.txt'); writeln(ans); DeleteFile(fname); writeln(fil eexists(fname)); writeln('Press return to end . . .'); readln; end. ---------------------------------------------------------------------- nugax · 2022-03-18 20:19 Actually, I was able to write a routine that closes the TInifile and writes the file, writes the comment, then opens the file back up. Easy enough to insert a comment. ---------------------------------------------------------------------- MarkMLl · 2022-03-18 20:37 Quote from: nugax on March 18, 2022, 09:19:41 pmActually, I was able to write a routine that closes the TInifile and writes the file, writes the comment, then opens the file back up. Easy enough to insert a comment. Yes, but you can't guarantee that the comment stays in the same place (or, for that matter, anywhere) after the next time the structured part of the file is written. MarkMLl ---------------------------------------------------------------------- nugax · 2022-03-18 20:45 Your right... hmmm..  except it doesnt write back to the inifile, just creates a default one. So, as of now, it only writes once. but if I do make changes it will overwrite it, i bet. ---------------------------------------------------------------------- MarkMLl · 2022-03-18 20:53 You'd be safest reimplementing with a TStringList... or even for a TStringList for the sections and a separate TStringList for the content of each section. You could potentially create a comment telling it that it was to try to stay after or before a particular entry, and save that as metadata. I've got a couple of units which extend the standard TIniFile with array-like and record-like facilities... MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-03-19 15:27 Quote from: Thaddy on March 18, 2022, 04:56:59 pmTextFiles is nonsense. The correct syntax is simply text. And the documentation is 100% correct (not 99.9). While TextFiles indead does not exist, TextFile does, namely as an alias for the Text type. This alias was introduced with Delphi, cause Text is a common property name. Quote from: BobDog on March 18, 2022, 08:49:51 pmSEEK(0...) would probably overwrite the first few characters anyway (as in c) Seek does not write anything, neither in Pascal nor in C.