FileWrite from StringGrid lazarus / 61-Beginners ---------------------------------------------------------------------- justnewbie · 2018-04-06 09:57 Hi! I need a procedure that writes some column's data of a StringGrid into a file. My code part: 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 TForm1.Button3Click(Sender: TObject);var  i: longint;  firstVal: byte;  lastVal: byte;    f: longint;  s: string;begin  if not DirectoryExists(IP_DIR_NAME) then CreateDir (IP_DIR_NAME);  lastVal := 0;  for i := 1 to StringGrid1.RowCount - 1 do  begin    firstVal:=StrToInt(StringGrid1.Cells[1, i]) div BASE_NUMBER;    if firstVal = lastVal then    begin    s:=StringGrid1.Cells[1, i]+' / '+StringGrid1.Cells[2, i];    FileWrite(f,s,SizeOf(s));    end    else begin    f:=FileCreate(IP_DIR_NAME+'/'+IntToStr(firstVal)+'.txt');    if f=-1 then Halt;    s:=StringGrid1.Cells[1, i]+' / '+StringGrid1.Cells[2, i];    FileWrite(f,s,SizeOf(s));    lastVal := firstVal;    end;  end;  FileClose(f);end; My problem is that it gives an unreadable file content (character encoding problem?). How can I get "normal", readable text files? Also, I need to separate the lines (new line for each new iteration). ---------------------------------------------------------------------- taazz · 2018-04-06 10:06 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  FileWrite(f,s[1],length(s));  and ofcourse 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 TForm1.Button3Click(Sender: TObject);const  crlf = #13#10;var  .  .  .   for i := 1 to StringGrid1.RowCount - 1 do  begin  .  .   .  FileWrite(f,crlf[1],2); end; ---------------------------------------------------------------------- justnewbie · 2018-04-06 10:14 @taazz: Thank you very much! Exactly what I need! ---------------------------------------------------------------------- justnewbie · 2018-04-06 10:25 A little different question: I have a CSV file and the (for concrete example) 158. line contains the first occurence of "MYVALUE" string. Can someone tell me, how can I get the line index (158) programmatically? ---------------------------------------------------------------------- wp · 2018-04-06 11:34 This questions requires additonal information: Do you need the index of the line in the file before loading into a StringGrid, StringList etc.?Or is the file already loaded and processed, e.g. displayed in a StringGrid?Is the index zero-based?Do you already know that the "MYVALUE" string is in line 158, or do you want to search the file (if not loaded yet) or the string grid (if already loaded) for the string and determine the index this way?Generally, a lot would be clearer if you would describe what you want to achieve in total. ---------------------------------------------------------------------- flowCRANE · 2018-04-06 11:42 @justnewbie: why are you using FileWrite? Use TFileStream and write strings in a convenient way. This class contains method WriteAnsiString (and more interesting), so you don't need to calculate string length. Additionally, end of line character(s) are stored in LineEnding constant, so you don't need to declare your own. Quote from: justnewbie on April 06, 2018, 12:25:37 pmCan someone tell me, how can I get the line index (158) programmatically? Line index or line (string) from index? You can load this file to the TStringList object and use the Strings property to get the access to the given line. ---------------------------------------------------------------------- taazz · 2018-04-06 12:00 Quote from: furious programming on April 06, 2018, 01:42:32 pm@justnewbie: why are you using FileWrite? Use TFileStream and write strings in a convenient way. This class contains method WriteAnsiString (and more interesting), so you don't need to calculate string length. writeansistring adds the length of the string in front of the string you are writing. Quote from: furious programming on April 06, 2018, 01:42:32 pmAdditionally, end of line character(s) are stored in LineEnding constant, so you don't need to declare your own. yeah I do, LineEnding is OS aware aka its #13#10 on windows and #10 on linux and macOS I'm not going to complicate a file format just to please some misplaced sense of whatever this is. ---------------------------------------------------------------------- justnewbie · 2018-04-06 12:07 Quote from: furious programming on April 06, 2018, 01:42:32 pm@justnewbie: why are you using FileWrite? Use TFileStream and write strings in a convenient way. This class contains method WriteAnsiString (and more interesting), so you don't need to calculate string length. Additionally, end of line character(s) are stored in LineEnding constant, so you don't need to declare your own. Quote from: justnewbie on April 06, 2018, 12:25:37 pmCan someone tell me, how can I get the line index (158) programmatically? Line index or line (string) from index? You can load this file to the TStringList object and use the Strings property to get the access to the given line. TFileStream >> Thank you for the tip, I will study it! ---------------------------------------------------------------------- justnewbie · 2018-04-06 12:09 Quote from: wp on April 06, 2018, 01:34:39 pmThis questions requires additonal information: Do you need the index of the line in the file before loading into a StringGrid, StringList etc.?Or is the file already loaded and processed, e.g. displayed in a StringGrid?Is the index zero-based?Do you already know that the "MYVALUE" string is in line 158, or do you want to search the file (if not loaded yet) or the string grid (if already loaded) for the string and determine the index this way?Generally, a lot would be clearer if you would describe what you want to achieve in total. Yes, I need this index before loading into the StringGrid. Attached an example of the CSV file (picture). I need to get the line index where the first occurence of "5555" is found (12 in this case. Line numbers are NOT the part of the CSV, of course). ---------------------------------------------------------------------- wp · 2018-04-06 12:43 The most memory-efficient method is using the csv parser of the unit csvreadwrite - see http://wiki.freepascal.org/CsvDocument (a bit out- dated, though). 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 TForm1.Button1Click(Sender: TObject);var  parser: TCSVParser;  stream: TFileStream;  s: String;begin  stream := TFileStream.Create('test.dat', fmOpenRead);  parser := TCSVParser.Create;  try    // Define the comma as field delimiter    parser.Delimiter := ',';    // Attach the data file stream to the parser    parser.SetSource(stream);    // Reset the parser (move stream pointer to the beginning) - not absolutely necessary    parser.ResetParser;    // Go from cell to cell. ParseNextCell is false after reading the last cell    while parser.ParseNextCell do begin    // Get the text of the cell just read by ParseNextCell      s := parser.CurrentCellText;      // check whether the search text is found.      // NOTE: Normal double-quotes are removed by the parser    if s = '5555' then begin        ShowMessage('Text found in line ' + IntToStr(parser.CurrentRow));        break;      end;    end;  finally    parser.Free;    stream.Free;  end;end; ---------------------------------------------------------------------- justnewbie · 2018-04-06 13:46 @wp: it is working like a dream! Awesome, thank you very much!  Just a quick question: How can I disable the form resizing possibility? I need a non- resizable form. ---------------------------------------------------------------------- wp · 2018-04-06 13:51 Change Form.BorderStyle to bsSingle or bsDialog depending on which system icons you need in the border. Play with these options to see the effect. ---------------------------------------------------------------------- justnewbie · 2018-04-06 14:05 Quote from: wp on April 06, 2018, 03:51:52 pmChange Form.BorderStyle to bsSingle or bsDialog depending on which system icons you need in the border. Play with these options to see the effect. Originally I used bsSingle, but could resize the window with mouse. Now it is OK. Strange. Thank you! ---------------------------------------------------------------------- wp · 2018-04-06 15:25 Be careful with nonsizeable forms. What looks fine on your system is maybe too tight on others, or when you application is translated to another language. Don't say this will not happen. ---------------------------------------------------------------------- flowCRANE · 2018-04-06 20:22 Quote from: taazz on April 06, 2018, 02:00:58 pmwriteansistring adds the length of the string in front of the string you are writing. Hmm… I got confused, sorry. Not TFileStream but TStringStream and not WriteAnsiString but WriteString. Quote from: taazz on April 06, 2018, 02:00:58 pmyeah I do, LineEnding is OS aware aka its #13#10 on windows and #10 on linux and macOS I'm not going to complicate a file format just to please some misplaced sense of whatever this is. Yes, it depends on platform, but LCL supports all of them, regardless of which one you use (line ending and OS). Quote from: justnewbie on April 06, 2018, 04:05:02 pmOriginally I used bsSingle, but could resize the window with mouse. Now it is OK. Strange. For sure? With bsSingle it is not possible to change the size of the window, neither using the mouse nor the keyboard.