<div dir="ltr">I&#39;ve been having trouble figuring out how to read and write data to a textfile, and not finding much out there for Swift on Linux.  I found code for reading data from a file on StackOverflow that works for me, but cludged together my own code for writing to a file.  I&#39;m not sure exactly what I&#39;m doing, but am making some progress.  Can anyone point me toward a good source of help for reading and writing data in Swift on Linux.  Thanks!<div><br></div><div>This is the code that I found that works for reading data:</div><div><span id="docs-internal-guid-f4279610-c58c-8124-ae91-2774412e47b8"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">//  Try to read from a file</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="color:rgb(0,0,0);font-family:Arial;font-size:14.6667px;white-space:pre-wrap;line-height:1.38;background-color:transparent">//  5/16/2016</span><br></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">import Glibc  //import a Linux library</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">let path = &quot;Resources/sampleIn.txt&quot;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">let BUFSIZE = 1024</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">print(&quot;attempting to open a file for input&quot;)</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">let fp = fopen(path, &quot;r&quot;)</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">if fp == nil {print(&quot;error reading file&quot;)}</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">if fp != nil {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">  print(&quot;reading...&quot;)</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">  var buf = [CChar](count:BUFSIZE, repeatedValue:CChar(0))</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">  while fgets(&amp;buf, Int32(BUFSIZE), fp) != nil {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">    print(String.fromCString(buf)!, terminator:&quot;&quot;)</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">  }</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">}</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><br></p></span></div><div>This is the code I pieced together for writing data:</div><div><pre class="" style="margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:Consolas,Menlo,Monaco,&#39;Lucida Console&#39;,&#39;Liberation Mono&#39;,&#39;DejaVu Sans Mono&#39;,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace,sans-serif;color:rgb(57,51,24);word-wrap:normal;background-color:rgb(239,240,241)"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,&#39;Lucida Console&#39;,&#39;Liberation Mono&#39;,&#39;DejaVu Sans Mono&#39;,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace,sans-serif;white-space:inherit"><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">import</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Glibc</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">  </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(133,140,147)">//import a Linux library</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">

</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">let</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> path </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">&quot;Resources/sampleOut.txt&quot;</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">let</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> BUFSIZE </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">1024</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">print</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">&quot;attempting to open a file for Output&quot;</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">let</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> fp </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> fopen</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">path</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">&quot;w+&quot;</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">if</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> fp </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">==</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">nil</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">{</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">print</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">&quot;error writing file&quot;</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)}</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">if</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> fp </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">!=</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">nil</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">{</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
  </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">print</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">&quot;Type a few words to be saved to a file&quot;</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
  </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">var</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> fewWords</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">readLine</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">()!</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
  fputs</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">fewWords</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">,</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">fp</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
  </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">print</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">&quot;writing...&quot;</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
  </span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">}</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
fclose</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">fp</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span></code></pre></div><div><br></div><div>Any help would be appreciated.</div><div>Thanks!</div><div>John<br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">John Myers<div>Mathematics and Computer Science Teacher</div><div><br></div><div>------------------------------------------------------------------</div><div><br></div></div></div>
</div></div>