<div dir="ltr">I'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'm not sure exactly what I'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 = "Resources/sampleIn.txt"</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("attempting to open a file for input")</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, "r")</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("error reading file")}</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("reading...")</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(&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:"")</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,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',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,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',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)">"Resources/sampleOut.txt"</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)">"attempting to open a file for Output"</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)">"w+"</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)">"error writing file"</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)">"Type a few words to be saved to a file"</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)">"writing..."</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>