Detach multiple strings on a single line separeted by a symbol lazarus / 61-Beginners ---------------------------------------------------------------------- Gald · 2021-01-29 03:49 Hello! I'm trying to read a text file like this: wpeqwensmdfn+aasdasd+sfdsdfsdfsdfsdfsdfawsfs+sdfsdfsadfsdafasdfsdfsf+f sdfsdfsd sdfsdf+sdfwqesfrrweftfsdfsdf+sdfwefsedfsdafsdf63+sdfsdafsadfsdfsdf+sdf sadfsadfsad 32sd1fsad1f+sdf1s35dfs23df2sdaf+15s6daf1sd5a3fsda3f5+safd561sdf1sdf+sd af4saf23sd I need something like the Copy2Symb function, but I need also to get the rest of the line to do it again and again(5x). What is the best way to do it? ---------------------------------------------------------------------- lucamar · 2021-01-29 04:52 The easier way is probably using a TStringList's AddDelimitedText() method. A simple example: 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 const  sample = 'wpeqwensmdfn+aasdasd+sfdsdfsdfsdf+sdfsfawsfs+sdfsdfsadfsdafa sdfsdfsf+fsdfsdfsd'+          'sdfsdf+sdfwqesfrrweftfsdfsdf+sdfwefsedf sdafsdf63+sdfsdafsadfsdfsdf+sdfsadfsadf'+           'sad32sd1fsad1f+sd f1s35dfs23df2sdaf+15s6daf1sd5a3fsda3f5+safd561sdf1sdf+sdaf4saf23sd';va r  AList: TStringList;begin  AList := TstringList.Create;  try    AList.AddDelimitedText(sample, '+', True)    {Now the strings in the list will contain:     wpeqwensmdfn    aasdasd    sfdsdfsdfsdf    etc.    Do what you will with them ...}  finally    AList.Free  end;end; You can first get your file into a string using ReadFileToString() (in unit fileutil) if it's not very large; otherwise, you can load it into yet another TStringList an get it from there through its Text property. If the file is really huge but is divided in lines then it's probably better to read each line and add it to the StringList until EOF(), lest you run out of memory (which can happen easily when processing huge text files) ---------------------------------------------------------------------- Gald · 2021-01-29 05:29 Quote from: lucamar on January 29, 2021, 05:52:46 am You can first get your file into a string using ReadFileToString() (in unit fileutil) if it's not very large; otherwise, you can load it into yet another TStringList an get it from there through its Text property. If the file is really huge but is divided in lines then it's probably better to read each line and add it to the StringList until EOF(), lest you run out of memory (which can happen easily when processing huge text files) Yes, the file is huge, 20GB. I saw here https://wiki.freepascal.org/File_Handling_In_Pascal On the following instruction: With larger files of many Gb, you may want to read in buffers of, say, 4096 bytes (you're advised to use a multiple of the filesytem cluster or block size) and do something with the data of each buffer read. 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  TotalBytesRead, BytesRead : Int64;  Buffer : array [0..4095] of byte;  // or, array [0..4095] of char  FileStream : TFileStream; try  FileStream := TFileStream.Create;  FileStream.Position := 0;  // Ensure you are at the start of the file  while TotalBytesRead <= FileStream.Size do  // While the amount of data read is less than or equal to the size of the stream do  begin    BytesRead := FileStream.Read(Buffer,sizeof(Buffer));  // Read in 4096 of data    inc(TotalBytesRead, BytesRead);                       // Increase TotalByteRead by the size of the buffer, i.e. 4096 bytes    // Do something with Buffer data  end; But unlike the rest of the page, there's no sample in particular of this FileStream. E.g. where to set the file path. I personally didn't understand that. I'm already reading line by line like the "Reading a textfile:" session of the link above. ---------------------------------------------------------------------- 440bx · 2021-01-29 07:01 Quote from: Gald on January 29, 2021, 06:29:35 amYes, the file is huge, 20GB. I'm already reading line by line like the "Reading a textfile:" session of the link above. When dealing with files that large, i.e, files that likely exceed available memory, it is necessary to "tailor" an algorithm to handle them in order to ensure performance is, at least, within reason. Reading a 20GB file a line at a time will, most likely, result in the program being very slow and a memory hog (it may not even work.)  A better way is to map a section of the file, something in the order of 750MB (so a 32 bit program can still handle it) one section at a time and, create an index of every "item" of interest in the file.  Note that even the above is workable only if the number of individual items in the file isn't very large.  If the number of individual items (separated by a "+" in your case) is itself large (indexing them exceeds a few hundred megabytes) then the index itself will need to be broken down into blocks.  Essentially, you're creating a database to process/manipulate the file. In your case, if the average length of an item of interest is 10 characters (and presuming single byte characters), the index alone would require well over 2GB (20GB div 10).  Obviously, that is a problem in itself and must be handled appropriately. The really important questions are : how does the information (individual item of interest) need to be accessed (serial read or direct access to individual elements) ?  Do they need to be updated (potentially be made larger or smaller) or just displayed (read only) ?  How important is responsiveness (is a user waiting for the operation to complete or is it done in batch) ?  is creating a new file consisting of fixed length "records" viable ?.  The answer to those questions and, possibly others, will determine what a good, workable, solution is. ---------------------------------------------------------------------- lucamar · 2021-01-29 07:07 Quote from: Gald on January 29, 2021, 06:29:35 amBut unlike the rest of the page, there's no sample in particular of this FileStream. E.g. where to set the file path. I personally didn't understand that. I'm already reading line by line like the "Reading a textfile:" session of the link above. No wonder you don't understand: that example is wrong (or "extremely simplified", if one wants to be caritative ). To create a file stream for reading one should use at least: 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 MyFileStream := TFileStream.Create('SomeDir/MyFile.ext', fmOpenRead)There you have where to set the file name. But if you're already reading line by line (using the old-style method with AssignFile, Reset, ReadLn, I assume) you don't need to complicate it more: you won't find things easier trying to use streams, since there is none specialized in reading text files, and though there are ways to cope with that (e.g. using the FCL's StreamIO or StreamEx units) they don't make your life that much easier. However, if you want some example of how it could be done, tell me and I'll prepare a quick one; I've nothing to do for the next two hours or so Added: 440bx answer came while I was typing; read it carefully, he's 100% right in what he says.