A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FFile%2FText_File</id>
		<title>C++/File/Text File - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FFile%2FText_File"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/File/Text_File&amp;action=history"/>
		<updated>2026-04-18T12:00:17Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/File/Text_File&amp;diff=880&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/File/Text_File&amp;diff=880&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/File/Text_File&amp;diff=881&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/File/Text_File&amp;diff=881&amp;oldid=prev"/>
				<updated>2010-05-25T10:24:05Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Read from file.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt; &lt;br /&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;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  ofstream out(&amp;quot;test&amp;quot;); &lt;br /&gt;
  if(!out) { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.\n&amp;quot;; &lt;br /&gt;
    return 1; &lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
  out &amp;lt;&amp;lt; 10 &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; 123.23 &amp;lt;&amp;lt; endl; &lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;This is a short text file.&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  out.close(); &lt;br /&gt;
&lt;br /&gt;
  char ch; &lt;br /&gt;
  int i; &lt;br /&gt;
  float f; &lt;br /&gt;
  char str[80]; &lt;br /&gt;
 &lt;br /&gt;
  ifstream in(&amp;quot;test&amp;quot;); &lt;br /&gt;
  if(!in) { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.\n&amp;quot;; &lt;br /&gt;
    return 1; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  in &amp;gt;&amp;gt; i; &lt;br /&gt;
  in &amp;gt;&amp;gt; f; &lt;br /&gt;
  in &amp;gt;&amp;gt; ch; &lt;br /&gt;
  in &amp;gt;&amp;gt; str; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; f &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; ch &amp;lt;&amp;lt; endl; &lt;br /&gt;
  cout &amp;lt;&amp;lt; str; &lt;br /&gt;
 &lt;br /&gt;
  in.close(); &lt;br /&gt;
  return 0; &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==reading a text file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &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;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main () {&lt;br /&gt;
  string line;&lt;br /&gt;
  ifstream myfile (&amp;quot;example.txt&amp;quot;);&lt;br /&gt;
  if (myfile.is_open())&lt;br /&gt;
  {&lt;br /&gt;
    while (! myfile.eof() )&lt;br /&gt;
    {&lt;br /&gt;
      getline (myfile,line);&lt;br /&gt;
      cout &amp;lt;&amp;lt; line &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
    myfile.close();&lt;br /&gt;
  }else &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Unable to open file&amp;quot;; &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Reading from a File==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
   char data[80];&lt;br /&gt;
   ofstream outfile;&lt;br /&gt;
   outfile.open(&amp;quot;students.dat&amp;quot;);&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Writing to the file&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Enter class name: &amp;quot;; &lt;br /&gt;
   cin.getline(data, 80);&lt;br /&gt;
   outfile &amp;lt;&amp;lt; data &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Enter number of students: &amp;quot;; &lt;br /&gt;
   cin &amp;gt;&amp;gt; data;&lt;br /&gt;
   cin.ignore();&lt;br /&gt;
   outfile &amp;lt;&amp;lt; data &amp;lt;&amp;lt; endl;&lt;br /&gt;
   outfile.close();&lt;br /&gt;
   ifstream infile; &lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Reading from the file&amp;quot; &amp;lt;&amp;lt; endl; &lt;br /&gt;
   infile.open(&amp;quot;students.dat&amp;quot;); &lt;br /&gt;
   infile &amp;gt;&amp;gt; data; &lt;br /&gt;
   cout &amp;lt;&amp;lt; data &amp;lt;&amp;lt; endl; &lt;br /&gt;
   infile &amp;gt;&amp;gt; data; &lt;br /&gt;
   cout &amp;lt;&amp;lt; data &amp;lt;&amp;lt; endl; &lt;br /&gt;
   infile.close();&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sequential Files==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main() &lt;br /&gt;
{&lt;br /&gt;
   char buffer[256];&lt;br /&gt;
   fstream myfile;&lt;br /&gt;
   myfile.open(&amp;quot;test2.txt&amp;quot;,ios::out | ios::trunc);&lt;br /&gt;
   if (myfile.is_open())&lt;br /&gt;
   {&lt;br /&gt;
        myfile &amp;lt;&amp;lt; &amp;quot;This outputting a line.\n&amp;quot;;&lt;br /&gt;
        myfile.close();&lt;br /&gt;
   }&lt;br /&gt;
   myfile.open(&amp;quot;test.txt&amp;quot;,ios::in);&lt;br /&gt;
   myfile.getline(buffer,100);&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;The file contains   &amp;quot; &amp;lt;&amp;lt; buffer &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
   myfile.close();&lt;br /&gt;
   myfile.open(&amp;quot;test.txt&amp;quot;,ios::app);&lt;br /&gt;
   myfile &amp;lt;&amp;lt; &amp;quot; Hey this is another line \n&amp;quot;;&lt;br /&gt;
   myfile.close();&lt;br /&gt;
   myfile.open(&amp;quot;test.txt&amp;quot;,ios::in);&lt;br /&gt;
   myfile.getline(buffer,200);&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;The file contains   &amp;quot; &amp;lt;&amp;lt; buffer &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
   myfile.close();&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use the getline function with the C++ string class==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt; &lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
   string data;&lt;br /&gt;
   ofstream outfile;&lt;br /&gt;
   outfile.open(&amp;quot;file.dat&amp;quot;);&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Writing to the file&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Enter class name: &amp;quot;; &lt;br /&gt;
   getline(cin, data); &lt;br /&gt;
   outfile &amp;lt;&amp;lt; data&amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Enter your id: &amp;quot;; &lt;br /&gt;
   cin &amp;gt;&amp;gt; data;&lt;br /&gt;
   cin.ignore();&lt;br /&gt;
   outfile &amp;lt;&amp;lt; data&amp;lt;&amp;lt; endl;&lt;br /&gt;
   outfile.close();&lt;br /&gt;
   ifstream infile; &lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Reading from the file&amp;quot; &amp;lt;&amp;lt; endl; &lt;br /&gt;
   infile.open(&amp;quot;file.dat&amp;quot;); &lt;br /&gt;
   getline(infile, data);&lt;br /&gt;
   cout &amp;lt;&amp;lt; data &amp;lt;&amp;lt; endl; &lt;br /&gt;
   getline(infile, data);&lt;br /&gt;
   cout &amp;lt;&amp;lt; data &amp;lt;&amp;lt; endl; &lt;br /&gt;
   infile.close();&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Write strings to disk==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &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;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  if(argc!=2) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Usage: WRITE &amp;lt;filename&amp;gt;\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  ofstream out(argv[1]); // output file&lt;br /&gt;
  if(!out) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open output file.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  char str[80];&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Write strings to disk, &amp;quot;$&amp;quot; to stop\n&amp;quot;;&lt;br /&gt;
  do {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;: &amp;quot;;&lt;br /&gt;
    cin &amp;gt;&amp;gt; str;&lt;br /&gt;
    out &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl;&lt;br /&gt;
  } while (*str != &amp;quot;$&amp;quot;);&lt;br /&gt;
  out.close();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Write string to a file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &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;
#include &amp;lt;cstring&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  ofstream out(&amp;quot;test&amp;quot;, ios::out | ios::binary);&lt;br /&gt;
  if(!out) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open output file.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  double num = 100.45;&lt;br /&gt;
  char str[] = &amp;quot;www.java2s.com&amp;quot;;&lt;br /&gt;
  out.write((char *) &amp;amp;num, sizeof(double));&lt;br /&gt;
  out.write(str, strlen(str));&lt;br /&gt;
  out.close();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Write to file: ofstream ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt; &lt;br /&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;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  ofstream out(&amp;quot;test&amp;quot;); &lt;br /&gt;
  if(!out) { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.\n&amp;quot;; &lt;br /&gt;
    return 1; &lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
  out &amp;lt;&amp;lt; 10 &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; 123.23 &amp;lt;&amp;lt; endl; &lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;This is a short text file.&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  out.close(); &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==writing on a text file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &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;
int main () {&lt;br /&gt;
  ofstream myfile (&amp;quot;example.txt&amp;quot;);&lt;br /&gt;
  if (myfile.is_open()){&lt;br /&gt;
    myfile &amp;lt;&amp;lt; &amp;quot;This is a line.\n&amp;quot;;&lt;br /&gt;
    myfile &amp;lt;&amp;lt; &amp;quot;This is another line.\n&amp;quot;;&lt;br /&gt;
    myfile.close();&lt;br /&gt;
  }&lt;br /&gt;
  else &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Unable to open file&amp;quot;;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Writing to a File==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
   char data[80];&lt;br /&gt;
   ofstream outfile;&lt;br /&gt;
   outfile.open(&amp;quot;students.dat&amp;quot;);&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Writing to the file&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Enter class name: &amp;quot;; &lt;br /&gt;
   cin.getline(data, 80);&lt;br /&gt;
   outfile &amp;lt;&amp;lt; data &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Enter number of students: &amp;quot;; &lt;br /&gt;
   cin &amp;gt;&amp;gt; data;&lt;br /&gt;
   cin.ignore();&lt;br /&gt;
   outfile &amp;lt;&amp;lt; data &amp;lt;&amp;lt; endl;&lt;br /&gt;
   outfile.close();&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>