<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>writng to a file | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/writng-to-a-file/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Wed, 01 Feb 2023 09:16:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>File I/O Functions Part -II</title>
		<link>https://studentprojects.in/software-development/cpp/file-i-o-functions-part-ii/</link>
					<comments>https://studentprojects.in/software-development/cpp/file-i-o-functions-part-ii/#respond</comments>
		
		<dc:creator><![CDATA[Shubhajna Rai]]></dc:creator>
		<pubDate>Fri, 03 Feb 2023 09:13:52 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[File I/O]]></category>
		<category><![CDATA[writng to a file]]></category>
		<guid isPermaLink="false">https://studentprojects.in/?p=10132</guid>

					<description><![CDATA[<p>C. Writing to a File Writing to a file in C++ is similar to printing output to the terminal. To write to a file, one creates an object of type ofstream and specifies the name and extension of the file. Then, use the insertion operator (&#60;&#60;) to add content to the file. Here&#8217;s an example</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/file-i-o-functions-part-ii/">File I/O Functions Part -II</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<h4>C. Writing to a File</h4>



<p>Writing to a file in C++ is similar to printing output to the terminal. To write to a file, one creates an object of type ofstream and specifies the name and extension of the file. Then, use the insertion operator (&lt;&lt;) to add content to the file.</p>



<p>Here&#8217;s an example to demonstrate how to write to a file.</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
 
using namespace std;
 
int main()
{
    string str = "Welcome_To_studentproject!";
    ofstream out("example.txt");
    out &lt;&lt; str;
    out.close();
    return 0;
}</code></pre>



<p>Output:</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">Welcome_To_studentproject!</code></pre>



<h4>D. Reading a file</h4>



<p>Reading from a file in C++ is similar to reading input from the terminal. To read from a file, you need to create an object of the type ifstream and specify the name of the file along with its extension. You then use the extraction operator (&gt;&gt;) to extract data from the file fed to the object. An example is provided to demonstrate how to read from a file.</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
 
using namespace std;
 
int main()
{
    string str;
    ifstream in("example.txt");
    in &gt;&gt; str;
    cout &lt;&lt; str;
    return 0;
}</code></pre>



<p>The file example.txt had “Welcome_To_studentproject!” as its content, hence the output:</p>



<p>Welcome_To_studentproject!</p><p>The post <a href="https://studentprojects.in/software-development/cpp/file-i-o-functions-part-ii/">File I/O Functions Part -II</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/file-i-o-functions-part-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
