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

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/Text_file_read&amp;diff=2970&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/Text_file_read&amp;diff=2970&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</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://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/Text_file_read&amp;diff=2971&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/Text_file_read&amp;diff=2971&amp;oldid=prev"/>
				<updated>2010-05-25T10:31:15Z</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;==file input with strings==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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;
  &lt;br /&gt;
int main(){  &lt;br /&gt;
   const int MAX = 80;&lt;br /&gt;
   char buffer[MAX];  &lt;br /&gt;
   ifstream infile(&amp;quot;TEST.TXT&amp;quot;); &lt;br /&gt;
   while( !infile.eof() ){  &lt;br /&gt;
      infile.getline(buffer, MAX); &lt;br /&gt;
      cout &amp;lt;&amp;lt; buffer &amp;lt;&amp;lt; endl;      &lt;br /&gt;
   }  &lt;br /&gt;
   return 0;  &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read a text file line by line==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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;
  ifstream myfile (&amp;quot;test.txt&amp;quot;);&lt;br /&gt;
  while (! myfile.eof() )&lt;br /&gt;
  {&lt;br /&gt;
    myfile.getline (buffer,100);&lt;br /&gt;
    cout &amp;lt;&amp;lt; buffer &amp;lt;&amp;lt; endl;&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;10 123.23&lt;br /&gt;
This is a text.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read from file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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;
  char ch; &lt;br /&gt;
  int i; &lt;br /&gt;
  float f; &lt;br /&gt;
  char str[80]; &lt;br /&gt;
 &lt;br /&gt;
  ofstream out(&amp;quot;test.txt&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; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;This is a text.&amp;quot;; &lt;br /&gt;
  out.close(); &lt;br /&gt;
  ifstream in(&amp;quot;test.txt&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; &amp;quot;\n&amp;quot;; &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;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;10 123.23 T&lt;br /&gt;
his&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reading and Writing Text Files==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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;INVNTRY&amp;quot;); // output, normal file&lt;br /&gt;
   &lt;br /&gt;
  if(!out) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open INVENTORY file.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;Radios &amp;quot; &amp;lt;&amp;lt; 39.95 &amp;lt;&amp;lt; endl;&lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;Toasters &amp;quot; &amp;lt;&amp;lt; 19.95 &amp;lt;&amp;lt; endl;&lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;Mixers &amp;quot; &amp;lt;&amp;lt; 24.80 &amp;lt;&amp;lt; endl;&lt;br /&gt;
   &lt;br /&gt;
  out.close();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read integer from 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;#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;stdexcept&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
void readIntegerFile(const string&amp;amp; fileName, vector&amp;lt;int&amp;gt;&amp;amp; dest)throw (invalid_argument, runtime_error)&lt;br /&gt;
{&lt;br /&gt;
  ifstream istr;&lt;br /&gt;
  int temp;&lt;br /&gt;
  istr.open(fileName.c_str());&lt;br /&gt;
  if (istr.fail()) {&lt;br /&gt;
    string error = &amp;quot;Unable to open file &amp;quot; + fileName;&lt;br /&gt;
    throw invalid_argument(error);&lt;br /&gt;
  }&lt;br /&gt;
  while (istr &amp;gt;&amp;gt; temp) {&lt;br /&gt;
    dest.push_back(temp);&lt;br /&gt;
  }&lt;br /&gt;
  if (istr.eof()) {&lt;br /&gt;
    istr.close();&lt;br /&gt;
  } else {&lt;br /&gt;
    istr.close();&lt;br /&gt;
    string error = &amp;quot;Unable to read file &amp;quot; + fileName;&lt;br /&gt;
    throw runtime_error(error);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
int main(int argc, char** argv)&lt;br /&gt;
{&lt;br /&gt;
  vector&amp;lt;int&amp;gt; myInts;&lt;br /&gt;
  const string fileName = &amp;quot;test.txt&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
    readIntegerFile(fileName, myInts);&lt;br /&gt;
  } catch (const invalid_argument&amp;amp; e) {&lt;br /&gt;
    cerr &amp;lt;&amp;lt; e.what() &amp;lt;&amp;lt; endl;&lt;br /&gt;
    exit (1);&lt;br /&gt;
  } catch (const runtime_error&amp;amp; e) {&lt;br /&gt;
    cerr &amp;lt;&amp;lt; e.what() &amp;lt;&amp;lt; endl;&lt;br /&gt;
    exit (1);&lt;br /&gt;
  }&lt;br /&gt;
  for (unsigned int i = 0; i &amp;lt; myInts.size(); i++) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; myInts[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return (0);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read string and float value 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;#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;
{&lt;br /&gt;
  ofstream out(&amp;quot;test.txt&amp;quot;); // output, normal file&lt;br /&gt;
  if(!out) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open test.txt file.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;R &amp;quot; &amp;lt;&amp;lt; 9.9 &amp;lt;&amp;lt; endl;&lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;T &amp;quot; &amp;lt;&amp;lt; 9.9 &amp;lt;&amp;lt; endl;&lt;br /&gt;
  out &amp;lt;&amp;lt; &amp;quot;M &amp;quot; &amp;lt;&amp;lt; 4.8 &amp;lt;&amp;lt; endl;&lt;br /&gt;
  out.close();&lt;br /&gt;
&lt;br /&gt;
  ifstream in(&amp;quot;test.txt&amp;quot;); // input&lt;br /&gt;
  if(!in) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open test.txt file.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  char item[20];&lt;br /&gt;
  float cost;&lt;br /&gt;
  in &amp;gt;&amp;gt; item &amp;gt;&amp;gt;  cost;&lt;br /&gt;
  cout &amp;lt;&amp;lt; item &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; cost &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
  in &amp;gt;&amp;gt; item &amp;gt;&amp;gt; cost;&lt;br /&gt;
  cout &amp;lt;&amp;lt; item &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; cost &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
  in &amp;gt;&amp;gt; item &amp;gt;&amp;gt; cost;&lt;br /&gt;
  cout &amp;lt;&amp;lt; item &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; cost &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
  in.close();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;R 9.9&lt;br /&gt;
T 9.9&lt;br /&gt;
M 4.8&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read text file line by line==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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;
   ifstream in(&amp;quot;text.txt&amp;quot;);&lt;br /&gt;
   if(!in){&lt;br /&gt;
      cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.&amp;quot;;&lt;br /&gt;
      exit (1);&lt;br /&gt;
   }&lt;br /&gt;
   char str[255];&lt;br /&gt;
   while(in){&lt;br /&gt;
      in.getline(str, 255);      // Delimiter defaults to newline&lt;br /&gt;
      cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl;&lt;br /&gt;
   }&lt;br /&gt;
   in.close();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read text file token by token==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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;
{&lt;br /&gt;
  ifstream inFile(&amp;quot;test.cpp&amp;quot;);&lt;br /&gt;
  if (inFile.fail()) {&lt;br /&gt;
    cerr &amp;lt;&amp;lt; &amp;quot;Unable to open file for reading.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  string nextToken;&lt;br /&gt;
  while (inFile &amp;gt;&amp;gt; nextToken) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Token: &amp;quot; &amp;lt;&amp;lt; nextToken &amp;lt;&amp;lt; endl;&lt;br /&gt;
  }&lt;br /&gt;
  inFile.close();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>