<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_I%2FO</id>
	<title>C++ I/O - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_I%2FO"/>
	<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=C%2B%2B_I/O&amp;action=history"/>
	<updated>2026-05-23T23:53:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://wiki.scott5.org/index.php?title=C%2B%2B_I/O&amp;diff=116&amp;oldid=prev</id>
		<title>Scott: Created page with &#039;==Terminal I/O== &lt;source lang=&quot;cpp&quot;&gt; #include &lt;iostream&gt;  // defines cout, cin, endl using namespace std; ... cout &lt;&lt; &quot;Enter your name: &quot;; cin &gt;&gt; name; cout &lt;&lt; &quot;Hi there, &quot; &lt;&lt; na…&#039;</title>
		<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=C%2B%2B_I/O&amp;diff=116&amp;oldid=prev"/>
		<updated>2011-01-31T19:06:53Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;#039;==Terminal I/O== &amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt; #include &amp;lt;iostream&amp;gt;  // defines cout, cin, endl using namespace std; ... cout &amp;lt;&amp;lt; &amp;quot;Enter your name: &amp;quot;; cin &amp;gt;&amp;gt; name; cout &amp;lt;&amp;lt; &amp;quot;Hi there, &amp;quot; &amp;lt;&amp;lt; na…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Terminal I/O==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;  // defines cout, cin, endl&lt;br /&gt;
using namespace std;&lt;br /&gt;
...&lt;br /&gt;
cout &amp;lt;&amp;lt; &amp;quot;Enter your name: &amp;quot;;&lt;br /&gt;
cin &amp;gt;&amp;gt; name;&lt;br /&gt;
cout &amp;lt;&amp;lt; &amp;quot;Hi there, &amp;quot; &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot;!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==File I/O==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
// reading a file:&lt;br /&gt;
void readFile(char* fileName){&lt;br /&gt;
    fstream infile(fileName);&lt;br /&gt;
    if (!infile.is_open()){&lt;br /&gt;
       cerr &amp;lt;&amp;lt; &amp;quot;failed to open file &amp;quot; &amp;lt;&amp;lt; fileName &amp;lt;&amp;lt; endl;&lt;br /&gt;
       exit(1);&lt;br /&gt;
    }&lt;br /&gt;
    const int maxLineSize = 256;&lt;br /&gt;
    char line[maxLineSize];&lt;br /&gt;
    int i = 0;&lt;br /&gt;
    while ( !infile.eof( ) ) {&lt;br /&gt;
        infile.getline(line, maxLineSize);&lt;br /&gt;
        cout &amp;lt;&amp;lt; &amp;quot;line&amp;quot; &amp;lt;&amp;lt; (i++) &amp;lt;&amp;lt; &amp;quot;: &amp;quot; &amp;lt;&amp;lt; line &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
// write to a file:&lt;br /&gt;
void writeStringToFile(char* fileName, string msg){&lt;br /&gt;
    ofstream outfile(fileName);&lt;br /&gt;
    if (!outfile.is_open()){&lt;br /&gt;
        cerr &amp;lt;&amp;lt; &amp;quot;failed to open file &amp;quot; &amp;lt;&amp;lt; fileName &amp;lt;&amp;lt; endl;&lt;br /&gt;
        exit(1);&lt;br /&gt;
    }&lt;br /&gt;
    outfile &amp;lt;&amp;lt; msg;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;string successfully written to file&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
int main(){&lt;br /&gt;
    readFile(&amp;quot;test.txt&amp;quot;);&lt;br /&gt;
    writeStringToFile(&amp;quot;test.txt&amp;quot;, &amp;quot;here is more text&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
//To append, use&lt;br /&gt;
//   ofstream outfile(fileName, ios::app);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Scott</name></author>
	</entry>
</feed>