[SOLVED] Write/Read to a file with specific x and y coordinates lazarus / 61-Beginners ---------------------------------------------------------------------- DiCri · 2016-09-12 21:34 Hi guys, i'd like to know if is it possible to write or read the file with x and y coords .. I use this: 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 var  data : textfile;  i : integer;  directory : string;begin ...Assign(Data, Directory );Reset(Data);Readln(Data, I );Close(Data);...end.  Imagine the file contains three lines with three numbers and i want to read only the second line and not the first Can i do that without creatig other variables? ---------------------------------------------------------------------- lainz · 2016-09-12 22:45 Use a TStringList. 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 var  s: TStringList;begin  // save  s := TStringList.Create;  s.Add('first line');  s.Add('second line');  s.Add('third line');  s.SaveToFile('file.txt');  s.Free;   // load  s := TStringList.Create;  s.LoadFromFile('file.txt');  writeln(s[1]); // second line  s.Free;end;     if you need to read many integers separated by spaces in the same line you can create a secondary stringlist. 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 s2 := TStringList.Create;s2.CommaText := s[1]; then you can acces each element from that line 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 s2[0] // first numbers2[1] // second number ---------------------------------------------------------------------- wp · 2016-09-12 22:56 Quote from: Manu12x on September 12, 2016, 11:34:58 pmImagine the file contains three lines with three numbers and i want to read only the second line and not the first Can i do that without creatig other variables? When reading the frist line don't specify any variable for the read input; this way this line will be discarded: 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 var  data: textfile;  i: integer;begin  AssignFile(Data, 'd:\test.txt');  Reset(Data);  Readln(Data);     // Read first line, but dispose result  ReadLn(Data, I);  // Read second line, store value in variable I  WriteLn(I);       // Just for a test: write out the value read  CloseFile(Data);end. ---------------------------------------------------------------------- Angus · 2016-09-12 22:58 Hi The TStringList approach will work very well until the file is large, then loading into the list will cause a noticeable performance hit.  Generally unless some prior knowledge is available regarding the location of the data in the file there is no choice but to 'scan' the file first to establish some basic information, not least of which would be to determine the number of lines; this would of course imply 'creating other variables'. For rapid access to a specific line(s) of the file it may be appropriate to use a database table and take advantage of the speed improvements afforded by use of an index.  Again there would be an additional variable introduced in the form of a table, but there-on-in retrieving data would be efficient. For large files memory management headaches would fall to the database engine to manage. ---------------------------------------------------------------------- lainz · 2016-09-12 23:05 Please he is a newby, give him a code that he can run like I did. Manu, if the file is not big, it will load faster anyway try it yourself. ---------------------------------------------------------------------- lainz · 2016-09-12 23:10 Try this application, it takes a second or less to save and load 1Million integers: How many you need for your game? 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 Project1; uses  Classes, SysUtils;var  s: TStringList;  i, j: integer; begin  s := TStringList.Create;  for i:=1 to 1000000 do  begin    s.Add(IntToStr(i));  end;  s.SaveToFile('file.txt');  s.Free;   s := TStringList.Create;  s.LoadFromFile('file.txt');  for i:=0 to s.Count-1 do  begin    j := StrToInt(s[i]);  end;  s.Free;  writeln('done');  readln();end. ---------------------------------------------------------------------- DiCri · 2016-09-13 04:15 Hey guys, i mean like gotoxy(x,y); but not on screen but while reading or writing to a file. File readed is < of 100KB.. Nothing more.. Only 10 integers ---------------------------------------------------------------------- DiCri · 2016-09-13 04:41 Quote from: lainz on September 13, 2016, 01:10:26 amTry this application, it takes a second or less to save and load 1Million integers: How many you need for your game? 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 Project1; uses  Classes, SysUtils;var  s: TStringList;  i, j: integer; begin  s := TStringList.Create;  for i:=1 to 1000000 do  begin    s.Add(IntToStr(i));  end;  s.SaveToFile('file.txt');  s.Free;   s := TStringList.Create;  s.LoadFromFile('file.txt');  for i:=0 to s.Count-1 do  begin    j := StrToInt(s[i]);  end;  s.Free;  writeln('done');  readln();end.  Hey lainz i didn't know about TStringList but it's so simple as i see! Thank you but what's s.Count? And if i want to write an integer = 450 then s[1] is 450 or 4?? ---------------------------------------------------------------------- lainz · 2016-09-13 14:18 s.Count is the ammount of items in the string list, the ammount of lines. If you want to write an integer you need to store it as string 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 var  num: integer; s.Add(IntToStr(num)); to read it convert it to number back 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 num := StrToInt(s[1]); ---------------------------------------------------------------------- DiCri · 2016-09-13 14:37 i solved : 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  var  OptionsStr : TStringList;  State : Integer;begin...if FileExists('Data\Options.txt' ) then  begin    OptionsStr := TStringList.Create;     OptionsStr.LoadFromFile('Data\Options.txt' );    State := StrToInt(OptionsStr[0]);   ... // here there are some functions that maybe can change value of variable "state".    OptionsStr[0] := IntToStr( State );    OptionsStr.SaveToFile('Data\Options.txt' );  end;...end;     That works correctly! Thanks! But maybe i should learn how to write and read data from files.. i see some games read data from a file wich contains unreadable chars! I don't know if i should use BlockWrite and BlockRead.. i don't understand them very well as i read in documentation.. thanks again ---------------------------------------------------------------------- wp · 2016-09-13 14:40 Quote from: Manu12x on September 13, 2016, 06:15:40 amHey guys, i mean like gotoxy(x,y); but not on screen but while reading or writing to a file. File readed is < of 100KB.. Nothing more.. Only 10 integers You are using a text file. Since the line length can vary there is no possibility to "goto" to some predefined line. You must declare the file such that it consits of records of predefined length (http://wiki.freepascal.org/File_Handling_In_Pascal). Then you can "seek" for a record at a given index. But note that index numbers begin with zero! Here's an example. For simplicity i assume that your file consists of "integers". Declare the file variable as a "file of integer". Now the reading/writing process accesses the file in blocks of 4 bytes because an integer is 4 bytes large. To seek a particular number you must know its index, like in an array. Since the index begins at 0 you must specify the index 1 if you seek for the second number. Note that there is no "ReadLn" or "WriteLn" for binary files because they don't contain "lines". 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 Project1; {$mode objfpc}{$H+} var  data : file of integer;  i : integer;begin  Assign(Data, 'd:\test.dat');   // Just prepare some dummy file  Rewrite(Data);  for i:=1 to 10 do    Write(Data, i);  CloseFile(Data);   // Open the file for reading  Reset(Data);  // Read the second number  // Go to the second number, it has index 1 because index number begin at zero.  Seek(Data, 1);    // Read one integer here to the variable I  Read(Data, I);  // Close the file  CloseFile(Data);   // Write out the found result to the console  // According to what was written to the file the second number should have the value 2  WriteLn(I);   ReadLn;end. ---------------------------------------------------------------------- lainz · 2016-09-13 14:43 These files are binary files not text files Here you go: http://www.delphibasics.co.uk/Article.asp?Name=Files Scroll down until 'Reading and writing to typed binary files' or 'Reading and writing to pure binary files' as you say it uses BlockRead and BlockWrite. ---------------------------------------------------------------------- DiCri · 2016-09-13 15:18 Quote from: wp on September 13, 2016, 04:40:07 pmQuote from: Manu12x on September 13, 2016, 06:15:40 amHey guys, i mean like gotoxy(x,y); but not on screen but while reading or writing to a file. File readed is < of 100KB.. Nothing more.. Only 10 integers You are using a text file. Since the line length can vary there is no possibility to "goto" to some predefined line. You must declare the file such that it consits of records of predefined length (http://wiki.freepascal.org/File_Handling_In_Pascal). Then you can "seek" for a record at a given index. But note that index numbers begin with zero! Here's an example. For simplicity i assume that your file consists of "integers". Declare the file variable as a "file of integer". Now the reading/writing process accesses the file in blocks of 4 bytes because an integer is 4 bytes large. To seek a particular number you must know its index, like in an array. Since the index begins at 0 you must specify the index 1 if you seek for the second number. Note that there is no "ReadLn" or "WriteLn" for binary files because they don't contain "lines". 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 Project1; {$mode objfpc}{$H+} var  data : file of integer;  i : integer;begin  Assign(Data, 'd:\test.dat');   // Just prepare some dummy file  Rewrite(Data);  for i:=1 to 10 do    Write(Data, i);  CloseFile(Data);   // Open the file for reading  Reset(Data);  // Read the second number  // Go to the second number, it has index 1 because index number begin at zero.  Seek(Data, 1);    // Read one integer here to the variable I  Read(Data, I);  // Close the file  CloseFile(Data);   // Write out the found result to the console  // According to what was written to the file the second number should have the value 2  WriteLn(I);   ReadLn;end.ok but : 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 Rewrite( Data ); this will delete all that the file contains because it's made for writing from beginning!  Thanks i only needed of "seek" as i see. I just want to substitute a line of the file with another integer.. ok try to see that code from my DinoLand game with SDL2 library: 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 ...procedure SDL_WriteCheckBox( Name : String; x, y : integer; Number : integer );  var    Rect : PSDL_Rect;    Clip : PSDL_Rect;    State : Integer;  begin  new( Rect );  new( Clip );    ... // here i change value of Rect and clip variables    if FileExists(' Data\Options.txt' ) then  begin  Assign( Data, GetCurrentDir + '\' + 'Data\Options.txt' );  Reset( Data );  // Here i should read integers ( data ) from the file and variable "Number" indicates the line of the file to read.  // So i don't know why.. i'll try with Seek();  // Here the variable "State" should take the value of the second line of the file.  Close( Data );  end; ...// After determinating "State" value, the CheckBox i want to create should be active when State = 1 and not active when State = 0 ( i already know how to do this part )// Here, this variable may will change, ( depends on mouse events ) ...   Assign( Data, GetCurrentDir + '\' + Data\Options.txt );  // Rewrite( Data );       Writeln( Data, State );  Close( Data );..end; beginrepeat  SDL_WriteCheckBox('show fps', 300, 50, 2 ); // name of the checkbox is 'show fps', x and y positions are 300 and 50, and '2' is the line i'd like to read State.until done;end. Options.txt is that: 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 0000 I just want to substitute the second line with the integer updated.. I hope you will understand me.. thanks... ---------------------------------------------------------------------- Handoko · 2016-09-13 15:42 I do not read this thread from top to end. But to answers your question, you can't just modify some data from certain position to certain position in the file easily, except you're using a database library, which they will do the complicated things in background. One of the ways to do it is read all of them to memory, modify them in the memory and write them all back to file. Database will work smarter because they will use caching technology to speed up the all the processes. I do not play a lot with database nor file. But I ever write my modules to handle this headache problems, and now I just use it without need to write it from zero. This is what it will do: To substitute line no 3 with new data 1. Open File_Source for reading and open File_Target for writing 2. Repeat 3.    Read a new line of data from File_Source 4.    If now is line 3 then write new data to File_Target 5.    If now is not line 3 then write the old data to File_Target 6. Until it reach end of file 7. Close File_Source and Close File_Target As I already mentioned I'm not good in file/database manipulation, some experts will give you better explanations. lainz already give a good link. But this one is good too: http://wiki.freepascal.org/File_Handling_In_Pascal ---------------------------------------------------------------------- wp · 2016-09-13 16:50 Quote from: Manu12x on September 13, 2016, 05:18:06 pmok but : 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 Rewrite( Data ); this will delete all that the file contains because it's made for writing from beginning!  Surely - this code is only needed by the example to get a file to work on. Quote from: Manu12x on September 13, 2016, 05:18:06 pmI just want to substitute a line of the file with another integer.If this file is a text file (what I suspect from the extension ".txt" in your code sample) then you are out of luck. Read the entire file into a string list, as lainz suggested - this is really very fast, believe me, you won't notice it. And then you have all the strings in the list ready for manipulation and re-saving. If this file is a binary file of - say - integers like in my previous example you certainly can edit a value in the file if the file is opened for reading and writing. You only must know that after every reading/writing process the file pointer advances to the next file item. This means: if you read index 1 the file will point to index 2 afterwards. Therefore you must call Seek again to go back to the index that you want to change. This example works: 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 Project1; {$mode objfpc}{$H+} var  data : file of integer;  i : integer;begin  Assign(Data, 'd:\test.dat');   // Just prepare some dummy file  Rewrite(Data);  for i:=1 to 10 do    Write(Data, i);  CloseFile(Data);   // Open the file for reading and writing  FileMode := 2;  // you can skip this - this is the default anyway. It prepares a binary file for read and write access.  Reset(Data);   // Read the second number   // Go to the second number, it has index 1 because index numbers begin at zero.  Seek(Data, 1);  // Read one integer here to the variable I  Read(Data, I);  // Write number to console: it should show "2"  WriteLn(I);   // Alter the number at index 1   // Go back to index 1  Seek(Data, 1);  // Write new value  Write(Data, 100);   // Read what's at index 1 now   // go back to index 1  Seek(Data, 1);  // Read value  Read(Data, I);  // Write to console - it should show "100"  WriteLn(I);   // Close the file  CloseFile(Data);   ReadLn;end. ---------------------------------------------------------------------- howardpc · 2016-09-13 19:48 If I have understood manu12x correctly I think he is looking for something like this to be incorporated in his program: 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 pTextFileState; {$mode objfpc}{H+} uses sysutils, classes; const  DataFileName = 'data' + PathDelim + 'Options.txt'; procedure SaveStateToTextFile(aState, aLineNumber: integer);var  sl: TStringList;begin  if not FileExists(GetCurrentDir + PathDelim + DataFileName) then    WriteLn('Missing data file "', DataFileName, '"')  else begin    sl:=TStringList.Create;    try      sl.LoadFromFile(DataFileName);      sl[aLineNumber - 1]:=IntToStr(aState);      sl.SaveToFile(DataFileName);    finally    sl.Free;    end;  end;end; function ReadStateFromTextFile(aLineNumber: integer): integer;var  sl: TStringList;begin  if not FileExists(GetCurrentDir + PathDelim + DataFileName) then    WriteLn('Missing data file "', DataFileName, '"')  else begin    sl:=TStringList.Create;    try      sl.LoadFromFile(DataFileName);      if not TryStrToInt(sl[aLineNumber - 1], Result)        then Result:= -1;    finally      sl.Free;    end;  end;end; procedure SDL_WriteCheckBox( Name : String; x, y : integer; Number : integer );var  Rect : PSDL_Rect;  Clip : PSDL_Rect;  State : Integer;begin  New( Rect );  New( Clip );   State:=ReadStateFromTextFile(Number);   // use or change State, Name, x, y ...   SaveStateToTextFile(State, Number);end; begin  SDL_WriteCheckBox('show fps', 300, 50, 2);  SDL_WriteCheckBox('show ...', 300, 70, 3);  SDL_WriteCheckBox('show dpi', 300, 90, 4);  // etc.end. ---------------------------------------------------------------------- DiCri · 2016-09-14 04:58 Quote from: howardpc on September 13, 2016, 09:48:29 pmIf I have understood manu12x correctly I think he is looking for something like this to be incorporated in his program: 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 pTextFileState; {$mode objfpc}{H+} uses sysutils, classes; const  DataFileName = 'data' + PathDelim + 'Options.txt'; procedure SaveStateToTextFile(aState, aLineNumber: integer);var  sl: TStringList;begin  if not FileExists(GetCurrentDir + PathDelim + DataFileName) then    WriteLn('Missing data file "', DataFileName, '"')  else begin    sl:=TStringList.Create;    try      sl.LoadFromFile(DataFileName);      sl[aLineNumber - 1]:=IntToStr(aState);      sl.SaveToFile(DataFileName);    finally    sl.Free;    end;  end;end; function ReadStateFromTextFile(aLineNumber: integer): integer;var  sl: TStringList;begin  if not FileExists(GetCurrentDir + PathDelim + DataFileName) then    WriteLn('Missing data file "', DataFileName, '"')  else begin    sl:=TStringList.Create;    try      sl.LoadFromFile(DataFileName);      if not TryStrToInt(sl[aLineNumber - 1], Result)        then Result:= -1;    finally      sl.Free;    end;  end;end; procedure SDL_WriteCheckBox( Name : String; x, y : integer; Number : integer );var  Rect : PSDL_Rect;  Clip : PSDL_Rect;  State : Integer;begin  New( Rect );  New( Clip );   State:=ReadStateFromTextFile(Number);   // use or change State, Name, x, y ...   SaveStateToTextFile(State, Number);end; begin  SDL_WriteCheckBox('show fps', 300, 50, 2);  SDL_WriteCheckBox('show ...', 300, 70, 3);  SDL_WriteCheckBox('show dpi', 300, 90, 4);  // etc.end.Yes something like! I won't write "try" and "finally" because,it says me there's no identifier.. ---------------------------------------------------------------------- Thaddy · 2016-09-14 07:22 The try/finally's are correct programming: that ensures the lists get free'd. What is you problem? Plz explain?