Reading files using delimiters/terminators

Ali Çehreli acehreli at yahoo.com
Sun Dec 27 04:11:51 UTC 2020


On 12/26/20 4:13 PM, Rekel wrote:
> I'm trying to read a file with entries seperated by '\n\n' (empty line), 
> with entries containing '\n'. I thought the 
> File.readLine(KeepTerminator, Terminator) might work, as it seems to 
> accept strings as terminators, since there seems to have been a thread 
> regarding '\r\n' seperators.
> 
> I don't know if there's some underlying reason, but when I try to use 
> "\n\n" as a terminator, I end up getting the entire file into 1 char[], 
> so it's not delimited.
> 
> Should this work or is there a reason one cannot use byLine like this?
> 
> For context, I'm trying this with the puzzle input of day 6 of this 
> year's advent of code. (https://adventofcode.com/)

byLine should work:

import std.stdio;

void main() {
   auto f = File("deneme.d");

   // Warning: byLine reuses an internal buffer. Call byLineCopy
   // if potentially parsed strings into the line need to persist.
   foreach (line; f.byLine) {
     if (line.length == 0) {
       writeln("EMPTY LINE");

     } else {
       writeln(line);
     }
   }
}

Ali



More information about the Digitalmars-d-learn mailing list