<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Drew</title>
    <description>The latest articles on DEV Community by Drew (@fill1890).</description>
    <link>https://dev.to/fill1890</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4253067%2F6ff73191-852b-4e86-b6d0-1f0461b57cbc.png</url>
      <title>DEV Community: Drew</title>
      <link>https://dev.to/fill1890</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fill1890"/>
    <language>en</language>
    <item>
      <title>On C++: Coroutines</title>
      <dc:creator>Drew</dc:creator>
      <pubDate>Thu, 19 Nov 2026 09:20:21 +0000</pubDate>
      <link>https://dev.to/fill1890/on-c-coroutines-40cd</link>
      <guid>https://dev.to/fill1890/on-c-coroutines-40cd</guid>
      <description>&lt;p&gt;One of the things I do in my day job is develop and maintain C++ firmware for ESP32-based devices. This may be the first post in a series about interesting concepts I encounter during my work on these projects (if I keep it up).&lt;/p&gt;

&lt;p&gt;Recently, I was developing a logging system: log specific events from the application to a file, then later process that file and upload it to an API for remote analytics. Events are stored on disk as JSON, but to mitigate corruption&lt;sup id="footnote-1-r"&gt;1&lt;/sup&gt; they're serialized to the log as single objects, one per line. When log files are rotated, each line is deserialized and appended to a JSON array, which is then written out to the "rotated" file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json5"&gt;&lt;code&gt;&lt;span class="c1"&gt;// active file&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"In the midst of chaos"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"there is also opportunity"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// rotated file&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"In the midst of chaos"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"there is also opportunity"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;My question is: during rotation, how does one iterate over the lines in the file (to read them as JSON objects)? The constraints are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Because of &lt;em&gt;reasons&lt;/em&gt;&lt;sup id="footnote-2-r"&gt;2&lt;/sup&gt;, we use the C &lt;code&gt;FILE*&lt;/code&gt; API for file I/O, with an &lt;a href="https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization" rel="noopener noreferrer"&gt;RAII&lt;/a&gt; wrapper class to provide nicer C++ access to data.&lt;/li&gt;
&lt;li&gt;While I could probably fit the entire file in memory, where's the fun in that? It's also not great practice, so instead we should try to lazily read and interpret lines from the file.&lt;sup id="footnote-3-r"&gt;3&lt;/sup&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our interface (and example usage) should look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nf"&gt;file_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nf"&gt;file_t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;filesystem&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/* string iterator type */&lt;/span&gt; &lt;span class="nf"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="kt"&gt;FILE&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;m_file&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;file_t&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"art_of_war.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;auto&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something with line, depending on the type&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;file_t&lt;/code&gt; already exists; we just need to add the &lt;code&gt;lines()&lt;/code&gt; method. Without coroutines&lt;sup id="footnote-4-r"&gt;4&lt;/sup&gt;, we have a couple of options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read all the lines into a &lt;code&gt;std::vector&amp;lt;std::string&amp;gt;&lt;/code&gt; (but we already decided we don't want to do this).&lt;/li&gt;
&lt;li&gt;Write an iterator adapter type. While this would work, it's frustrating: it adds another 1-2 types, state has to be dealt with, iterators are (in my experience) annoying to write, and satisfying the various concepts to make it range-adaptable (a very useful goal) can be fiddly.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  A Brief Sidebar to Extol the Virtues of Ranges
&lt;/h3&gt;

&lt;p&gt;Something I've done in other situations is written a function that returns an entire type-deduced range adapter. For example, if we have a file logger class that logs and rotates in a specified folder, we may want to iterate through all of the files that have been rotated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nf"&gt;file_logger_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nf"&gt;file_logger_t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;filesystem&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="n"&gt;directory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_dir&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;directory&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;auto&lt;/span&gt; &lt;span class="nf"&gt;logs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;filesystem&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directory_iterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_dir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
           &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
               &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;auto&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;".json"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;filesystem&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="n"&gt;m_dir&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can then be used like &lt;code&gt;for(const auto&amp;amp; entry : logger.logs())&lt;/code&gt;. Ranges are a nice (although often fairly verbose) way to do declarative, lazy-evaluated iteration. Unfortunately this technique can't be applied directly, as we don't have a built-in way to convert a &lt;code&gt;FILE*&lt;/code&gt; into a range of lines (being the entire point of this post).&lt;/p&gt;

&lt;h3&gt;
  
  
  Back to Coroutines
&lt;/h3&gt;

&lt;p&gt;As someone who also uses Python frequently, the obvious thing to use here is a generator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"But alas," I thought, "I can't do this in C++". Fortunately, I was wrong, and happened to stumble across &lt;a href="https://en.cppreference.com/cpp/coroutine/generator" rel="noopener noreferrer"&gt;&lt;code&gt;std::generator&lt;/code&gt;&lt;/a&gt;, and more generally C++ coroutines.&lt;/p&gt;

&lt;h3&gt;
  
  
  She co on my routine till I yield
&lt;/h3&gt;

&lt;p&gt;Allow me to give you a brief overview of coroutines, in case you haven't encountered them before. In slightly more formal/traditional/old computing, a function is referred to as a "subroutine": a section of code ("routine") that runs inside another ("sub"). Execution enters the subroutine, some stuff happens, then execution leaves, possibly with a return value.&lt;/p&gt;

&lt;p&gt;Coroutines, on the other hand, maintain an internal state:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The caller invokes the coroutine.&lt;/li&gt;
&lt;li&gt;The coroutine can, at any point, &lt;em&gt;yield&lt;/em&gt;, returning execution to the caller, potentially with a value. Critically, &lt;strong&gt;all variables are saved&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The caller does some other stuff.&lt;/li&gt;
&lt;li&gt;The caller returns control to the coroutine.&lt;/li&gt;
&lt;li&gt;The coroutine then resumes from the point it yielded earlier, with  all variables containing the same value as was saved earlier.&lt;/li&gt;
&lt;li&gt;This cycle repeats from 2.&lt;/li&gt;
&lt;li&gt;Eventually, the coroutine may return, similarly to a normal function. At this point the variables are discarded.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Coroutines are often referred to as "functions which can be paused and resumed". (They can sort of be approximated with static variables inside functions, but static variables have global state across all invocations, and can't be easily reset).&lt;/p&gt;

&lt;h3&gt;
  
  
  Feel free to get the point anytime
&lt;/h3&gt;

&lt;p&gt;The upshot of this is the C++ implementation is nearly as simple as the Python one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nf"&gt;file_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nf"&gt;file_t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;filesystem&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;generator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string_view&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;fseek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SEEK_SET&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fgets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;co_yield&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string_view&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="kt"&gt;FILE&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;m_file&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(This doesn't have to implemented in a header file and inline within the class; I've just done so to make it easier).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I've used &lt;code&gt;fgets()&lt;/code&gt; to read until a newline. This implementation assumes all lines are less than 4096 characters long.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;std::string_view&lt;/code&gt; doesn't copy the string from &lt;code&gt;line&lt;/code&gt;, so it's faster than &lt;code&gt;std::string&lt;/code&gt; (it's effectively returning &lt;code&gt;line*&lt;/code&gt; with a nice wrapper).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fgets()&lt;/code&gt; returns &lt;code&gt;NULL&lt;/code&gt; on EOF or error. This implementation also assumes we don't encounter an error.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Disclaimers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;I am not nor am I claiming to be a C++ expert.&lt;/li&gt;
&lt;li&gt;Are there other ways to solve this problem? Certainly! This is just the one I chose to use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to discuss, critique my code, ask questions, etc :)&lt;/p&gt;

&lt;p&gt;1. Hopefully.&lt;br&gt;
2. &lt;code&gt;#include &amp;lt;iostream&amp;gt;&lt;/code&gt; adds about 200 kB to the binary size.&lt;br&gt;
3. Mind you we are storing the entire JSON array in memory, so this is kind of a moot point, but we're sticking with it for the sake of the argument.&lt;br&gt;
4. Because I hadn't heard of them yet&lt;/p&gt;

</description>
      <category>cpp</category>
    </item>
  </channel>
</rss>
