<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: 13.7 &#8212; Random file I/O</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/137-random-file-io/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/</link>
	<description></description>
	<lastBuildDate>Thu, 02 Feb 2012 21:20:10 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: loift</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-95906</link>
		<dc:creator>loift</dc:creator>
		<pubDate>Sun, 21 Aug 2011 06:56:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-95906</guid>
		<description>I need to do many manipulations on a file on a bit level. Is there a better way to do this than: (1)open the file in input mode (2)open a second file in output mode (3)read the input file as a string (4)individually convert each character of this string to its binary (ascii/utf-8) value and append/write this to the output file (5)do manipulations on the output file (6) manually convert the output file back by reading the output file as a string 8 &quot;boolean&quot; characters at a time and turning it into an ascii/utf-8 value.</description>
		<content:encoded><![CDATA[<p>I need to do many manipulations on a file on a bit level. Is there a better way to do this than: (1)open the file in input mode (2)open a second file in output mode (3)read the input file as a string (4)individually convert each character of this string to its binary (ascii/utf-8) value and append/write this to the output file (5)do manipulations on the output file (6) manually convert the output file back by reading the output file as a string 8 &#8220;boolean&#8221; characters at a time and turning it into an ascii/utf-8 value.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: paras</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-95832</link>
		<dc:creator>paras</dc:creator>
		<pubDate>Sun, 07 Aug 2011 08:24:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-95832</guid>
		<description>hi..I want to find whether a string is present or not in  a file which i have already written and its contents are being displayed...can someone help me with the code...</description>
		<content:encoded><![CDATA[<p>hi..I want to find whether a string is present or not in  a file which i have already written and its contents are being displayed&#8230;can someone help me with the code&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Casey</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-93052</link>
		<dc:creator>Casey</dc:creator>
		<pubDate>Fri, 01 Oct 2010 15:35:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-93052</guid>
		<description>I figured out my problem. The ios namespace lives inside std, so to use ios, I must also use std. My fstream parameters were being declared outside of their scope.

Man, this stuff gets confusing!</description>
		<content:encoded><![CDATA[<p>I figured out my problem. The ios namespace lives inside std, so to use ios, I must also use std. My fstream parameters were being declared outside of their scope.</p>
<p>Man, this stuff gets confusing!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Casey</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-93016</link>
		<dc:creator>Casey</dc:creator>
		<pubDate>Fri, 01 Oct 2010 00:07:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-93016</guid>
		<description>Is it possible to pass fstream objects as parameters? I am writing a simple function that calculates the size of a file stream passed to it. Here is the code.

&lt;pre&gt;
//Obtain the size of the file without moving the file pointer
unsigned long fileSize(fstream file)
{
	unsigned long position = file.tellg();

	//Move pointer to end
	file.seekg(0, ios::end);

	//Record size in bytes
	unsigned long size = file.tellg();

	//Return file pointer to original location
	file.seekg(position, ios::beg);

	return size;
}
&lt;/pre&gt;


Any help is appreciated. 

Also, is there a tutorial on advanced operations with binary files? I&#039;m writing a file archive application and I need a little help with the C++ filestream objects.
I would like to weave files together byte by byte. I would also like to write variables, like the number and names of files in the archive, to the top of the file for easy reference.

Thank you for these tutorials. They have been a great resource for me.</description>
		<content:encoded><![CDATA[<p>Is it possible to pass fstream objects as parameters? I am writing a simple function that calculates the size of a file stream passed to it. Here is the code.</p>
<pre>
//Obtain the size of the file without moving the file pointer
unsigned long fileSize(fstream file)
{
	unsigned long position = file.tellg();

	//Move pointer to end
	file.seekg(0, ios::end);

	//Record size in bytes
	unsigned long size = file.tellg();

	//Return file pointer to original location
	file.seekg(position, ios::beg);

	return size;
}
</pre>
<p>Any help is appreciated. </p>
<p>Also, is there a tutorial on advanced operations with binary files? I&#8217;m writing a file archive application and I need a little help with the C++ filestream objects.<br />
I would like to weave files together byte by byte. I would also like to write variables, like the number and names of files in the archive, to the top of the file for easy reference.</p>
<p>Thank you for these tutorials. They have been a great resource for me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tobi</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-73426</link>
		<dc:creator>Tobi</dc:creator>
		<pubDate>Tue, 01 Dec 2009 19:33:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-73426</guid>
		<description>Thanks, was wondering why this code snippet didn&#039;t work for me but with binary mode it worked flawlessly:)</description>
		<content:encoded><![CDATA[<p>Thanks, was wondering why this code snippet didn&#8217;t work for me but with binary mode it worked flawlessly:)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ebrahim</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-72221</link>
		<dc:creator>Ebrahim</dc:creator>
		<pubDate>Fri, 13 Nov 2009 13:28:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-72221</guid>
		<description>Hello
why this code is true?
&lt;pre&gt;

#include&lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;

int main()
{
	int b[4]={1,1,5,1};
	int c[4]={0};
	fstream A(&quot;File.txt&quot;,ios::binary&#124;ios::in&#124;ios::out);
	if(!A)
	{
		return 1;
	}
	A.write((char *) (&amp;b),sizeof(b));//(&amp;b) must be (b) but ?????!!!!!
	A.seekg(0);
	A.read((char *) (c),sizeof(c));
	cout&lt;&lt;c[2];
	return 0;
}

&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hello<br />
why this code is true?</p>
<pre>

#include&lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;

int main()
{
	int b[4]={1,1,5,1};
	int c[4]={0};
	fstream A(&quot;File.txt&quot;,ios::binary|ios::in|ios::out);
	if(!A)
	{
		return 1;
	}
	A.write((char *) (&amp;b),sizeof(b));//(&amp;b) must be (b) but ?????!!!!!
	A.seekg(0);
	A.read((char *) (c),sizeof(c));
	cout&lt;&lt;c[2];
	return 0;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ebrahim</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-72220</link>
		<dc:creator>Ebrahim</dc:creator>
		<pubDate>Fri, 13 Nov 2009 13:27:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-72220</guid>
		<description>Hello
why this code is true?
&lt;pre&gt;

#include
#include 
using namespace std;

int main()
{
	int b[4]={1,1,5,1};
	int c[4]={0};
	fstream A(&quot;File.txt&quot;,ios::binary&#124;ios::in&#124;ios::out);

	if(!A)
	{
		return 1;
	}
	A.write((char *) (&amp;b),sizeof(b));//(&amp;b) must be (b) but ?????!!!!!
	A.seekg(0);
	A.read((char *) (c),sizeof(c));
	cout&lt;&lt;c[2];
	return 0;
}</description>
		<content:encoded><![CDATA[<p>Hello<br />
why this code is true?</p>
<pre>

#include
#include
using namespace std;

int main()
{
	int b[4]={1,1,5,1};
	int c[4]={0};
	fstream A("File.txt",ios::binary|ios::in|ios::out);

	if(!A)
	{
		return 1;
	}
	A.write((char *) (&amp;b),sizeof(b));//(&amp;b) must be (b) but ?????!!!!!
	A.seekg(0);
	A.read((char *) (c),sizeof(c));
	cout&lt;&lt;c[2];
	return 0;
}</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: dekaya</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-61010</link>
		<dc:creator>dekaya</dc:creator>
		<pubDate>Mon, 18 May 2009 04:51:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-61010</guid>
		<description>I modified your code slightly to get it to work on my win2k machine.
using dev-cpp

I added :

&lt;pre&gt; 
#include &lt;fstream&gt;// ***  added for fstreams


using namespace std; //moved outside of main

//and finally opened in binary mode
fstream iofile(&quot;Sample.dat&quot;, ios::binary &#124; ios::in &#124; ios::out);
// **** the binary part is important otherwise the position is off

&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I modified your code slightly to get it to work on my win2k machine.<br />
using dev-cpp</p>
<p>I added :</p>
<pre>
#include &lt;fstream&gt;// ***  added for fstreams

using namespace std; //moved outside of main

//and finally opened in binary mode
fstream iofile(&quot;Sample.dat&quot;, ios::binary | ios::in | ios::out);
// **** the binary part is important otherwise the position is off
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 14.1 &#8212; Function templates</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-30957</link>
		<dc:creator>Learn C++ - &#187; 14.1 &#8212; Function templates</dc:creator>
		<pubDate>Sat, 25 Oct 2008 17:10:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-30957</guid>
		<description>[...] 2007      Prev/Next Posts   &#171; 13.7 &#8212; Random file I/O &#124; Home &#124; 14.2 &#8212; Function template instances &#187;     Friday, April 18th, 2008 at 4:54 [...]</description>
		<content:encoded><![CDATA[<p>[...] 2007      Prev/Next Posts   &laquo; 13.7 &#8212; Random file I/O | Home | 14.2 &#8212; Function template instances &raquo;     Friday, April 18th, 2008 at 4:54 [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/137-random-file-io/comment-page-1/#comment-27041</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Thu, 11 Sep 2008 07:23:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=195#comment-27041</guid>
		<description>Hmmm, that is interesting about seekg(0, ios::cur) not working for you.  It worked fine for me, but maybe it is being optimized away in your case as you suggest.

I added a short blurb about tellg() and tellp() to the tutorial and also changed the example to use &lt;code&gt;iofile.seekg(iofile.tellg(), ios::beg);&lt;/code&gt;, though I do have some concerns about the performance ramificaitons of doing such a thing (not sure if it&#039;s smart enough to convert that into a relative position, or whether it&#039;s going back to the beginning of the file each time and then counting it out).

As for the seekg()/seekp() difference, as far as I can tell with fstream they appear to be identical.</description>
		<content:encoded><![CDATA[<p>Hmmm, that is interesting about seekg(0, ios::cur) not working for you.  It worked fine for me, but maybe it is being optimized away in your case as you suggest.</p>
<p>I added a short blurb about tellg() and tellp() to the tutorial and also changed the example to use <code>iofile.seekg(iofile.tellg(), ios::beg);</code>, though I do have some concerns about the performance ramificaitons of doing such a thing (not sure if it&#8217;s smart enough to convert that into a relative position, or whether it&#8217;s going back to the beginning of the file each time and then counting it out).</p>
<p>As for the seekg()/seekp() difference, as far as I can tell with fstream they appear to be identical.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

