<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>textfile usage...</title>
		<link>http://www.allegro.cc/forums/view/578742</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 14 Apr 2006 16:19:07 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ive been looking around alot on functions for using text files, and ive found some stuff, but its not really straight forward... ive found these functions, but im not sure which is the best to use...</p><p>fputs();<br />fgets();<br />fopen();<br />fwrite();<br />fread();<br />fclose();</p><p>so far ive figured out how to use fopen, fwrite, and then fclose to close it... but im having troubles on fread, and i dont think im doing fwrite properly...</p><p>i was gonna have some text read from a file, and printed to the screen using textprintf_ex. but i couldnt get fread to work...</p><p>can someone please explain to me how fwrite, and fread work?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MSGleader)</author>
		<pubDate>Wed, 12 Apr 2006 06:54:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Please show the code you&#39;ve got, and we can go from there.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Wed, 12 Apr 2006 07:58:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/PACKFILE" target="_blank"><span class="a">PACKFILE</span></a> <span class="k3">*</span>pf <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_fopen" target="_blank"><span class="a">pack_fopen</span></a><span class="k2">(</span><span class="s">"file.txt"</span>, <span class="s">"w"</span><span class="k2">)</span><span class="k2">;</span>  <span class="c">// open file for writing</span>
<span class="k1">char</span> data<span class="k2">[</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">"Hello world!"</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span><span class="k2">(</span><span class="k1">void</span> <span class="k3">*</span><span class="k2">)</span>data, <a href="http://www.delorie.com/djgpp/doc/libc/libc_764.html" target="_blank">strlen</a><span class="k2">(</span>data<span class="k2">)</span>, pf<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/pack_fclose" target="_blank"><span class="a">pack_fclose</span></a><span class="k2">(</span>pf<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

That would write the string &quot;Hello world!&quot; to a file called &quot;file.txt&quot;. To read the string back from the file you would do the following:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/PACKFILE" target="_blank"><span class="a">PACKFILE</span></a> <span class="k3">*</span>pf <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_fopen" target="_blank"><span class="a">pack_fopen</span></a><span class="k2">(</span><span class="s">"file.txt"</span>, <span class="s">"r"</span><span class="k2">)</span><span class="k2">;</span>  <span class="c">// open for reading</span>
<span class="k1">if</span> <span class="k2">(</span>pf<span class="k2">)</span> <span class="k2">{</span>  <span class="c">// if pf == NULL, the file does not exist</span>
    <span class="k1">char</span> data<span class="k2">[</span><span class="n">256</span><span class="k2">]</span><span class="k2">;</span>
    <span class="k1">int</span> i<span class="k2">;</span>
    <span class="k1">for</span> <span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> <span class="k3">!</span><a href="http://www.allegro.cc/manual/pack_feof" target="_blank"><span class="a">pack_feof</span></a><span class="k2">(</span>pf<span class="k2">)</span><span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span> i<span class="k2">)</span> <span class="k2">{</span>
        data<span class="k3">&lt;</span>i&gt; <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_getc" target="_blank"><span class="a">pack_getc</span></a><span class="k2">(</span>pf<span class="k2">)</span><span class="k2">;</span>
    <span class="k2">}</span>
    data<span class="k2">[</span>i <span class="k3">+</span> <span class="n">1</span><span class="k2">]</span> <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
<a href="http://www.allegro.cc/manual/pack_fclose" target="_blank"><span class="a">pack_fclose</span></a><span class="k2">(</span>pf<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonny Cook)</author>
		<pubDate>Wed, 12 Apr 2006 08:04:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre>        <span class="k1">char</span> words<span class="k2">[</span><span class="n">5</span><span class="k2">]</span><span class="k2">;</span>
  FILE <span class="k3">*</span>thefile <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_337.html" target="_blank">fopen</a><span class="k2">(</span><span class="s">"stuff.txt"</span>,<span class="s">"a"</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.delorie.com/djgpp/doc/libc/libc_371.html" target="_blank">fwrite</a><span class="k2">(</span><span class="s">"Joely"</span>,<span class="n">5</span>,<span class="n">1</span>,thefile<span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.delorie.com/djgpp/doc/libc/libc_308.html" target="_blank">fclose</a><span class="k2">(</span>thefile<span class="k2">)</span><span class="k2">;</span>
  thefile <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_337.html" target="_blank">fopen</a><span class="k2">(</span><span class="s">"stuff.txt"</span>,<span class="s">"r"</span><span class="k2">)</span><span class="k2">;</span>
  <span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span>
          <span class="k1">if</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ENTER<span class="k2">]</span><span class="k2">)</span><span class="k2">{</span>
                             <a href="http://www.delorie.com/djgpp/doc/libc/libc_349.html" target="_blank">fread</a><span class="k2">(</span>words,<span class="n">5</span>,<span class="n">1</span>,thefile<span class="k2">)</span><span class="k2">;</span>
                             <a href="http://www.allegro.cc/manual/textprintf_ex" target="_blank"><span class="a">textprintf_ex</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,<a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>,<span class="n">10</span>,<span class="n">10</span>,<a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">255</span>,<span class="n">255</span><span class="k2">)</span>,<span class="k3">-</span><span class="n">1</span>,<span class="s">"Text file contents: %s"</span>,words<span class="k2">)</span><span class="k2">;</span>
                             <span class="k2">}</span>
  <span class="k2">}</span>
</pre></div></div><p>


^I was just experimenting... but thats what i have in my code...<br />ive gotten fread to sortof work now...<br />and im not using packfiles... will it still work?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MSGleader)</author>
		<pubDate>Wed, 12 Apr 2006 08:19:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>For the love of Shawn don&#39;t use fread and fwrite!<br />To read a line from a textfile, use fgets(). If you need to chop it up in smaller parts, you can do that afterwards.<br />To write lines to a testfile, just use fprintf(). fwrite() is probably ok for a character string, but I tend to make the logical distinction between <i>binary data</i> and <i>text</i> (which is of course stored as binary data) and stick with using the proper I/O functions for either.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 12 Apr 2006 11:49:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The differences between binary and text files, from a C point of view, are rather confusing for beginners. In fact, the only reason why they are treated differently is the fact that different platforms expect different codes for line breaks (LF or CR or both), which the i/o functions handle for you if it&#39;s a text file, but not if it&#39;s a binary file. <br />After all, a string in C is nothing but an array of chars, so you can just read the characters one by one using fgetc. Sometimes, this can be the most straightforward solution, especially when you don&#39;t care about line breaks (for example, a C parser would ignore line breaks, since all whitespace is treated equal in C - at least after preprocessing).<br />The most important reason for not using fread / fwrite is that they read/write memory contents to/from disk &quot;as-is&quot;, without taking into account the fact that different platforms use different (incompatible) internal data representations. For example, C mandates that a char has <i>at least</i> 8 bits, but it may have more.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 12 Apr 2006 12:03:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ok, so ive gotten fprintf to work, and ive gotten the fgets to work... the only problem im having now is when i use return(enter) in the text file... this happens</p><p>The text file:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> HumanPlayer
<span class="k2">{</span>
<span class="k1">int</span> x,y<span class="k2">;</span>
<span class="k1">int</span> alive<span class="k2">;</span>
<span class="k1">int</span> life<span class="k2">;</span>
<span class="k2">}</span>player<span class="k2">;</span>
</pre></div></div><p>

This is what prints to the screen...
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> HumanPlayer^
<span class="k2">{</span>^
<span class="k1">int</span> x,y<span class="k2">;</span>^
<span class="k1">int</span> alive<span class="k2">;</span>^
<span class="k1">int</span> life<span class="k2">;</span>^
<span class="k2">}</span>player<span class="k2">;</span>
</pre></div></div><p>

how come it keeps putting a stupid ^ symbol for every return i use?<br />it wont do that when i actually load the text file for my game code will it?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MSGleader)</author>
		<pubDate>Wed, 12 Apr 2006 22:08:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I bet it&#39;s the carriage return character, which is at the end of every line in windows. However, it should be removed if you use text mode instead of binary mode.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Fladimir da Gorf)</author>
		<pubDate>Wed, 12 Apr 2006 22:49:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>...in fact, this is the only thing you need &quot;text files&quot; for.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 14 Apr 2006 16:19:07 +0000</pubDate>
	</item>
</rss>
