<!DOCTYPE html>
	<html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
	<head>
<title>&#91;C++ Cơ bản&#93; Phần 8&#x3A; Input và Output &#40;tiếp&#41;</title>
<meta name="description" content="&#91;C++ Cơ bản&#93; Phần 8&#x3A; Input và Output &#40;tiếp&#41; - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;tinhocdct.xyz&#x002F;savefile&#x002F;hoat-dong-chuyen-mon&#x002F;c-co-ban-phan-8-input-va-output-tiep-100.html">
<meta name="author" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG">
<meta name="copyright" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG [webmaster@tinhocdct.xyz]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="&#91;C++ Cơ bản&#93; Phần 8&#x3A; Input và Output &#40;tiếp&#41;">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;tinhocdct.xyz&#x002F;savefile&#x002F;hoat-dong-chuyen-mon&#x002F;c-co-ban-phan-8-input-va-output-tiep-100.html">
<meta property="og:site_name" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG">
<meta property="og:url" content="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-8-input-va-output-tiep-100.html">
<link rel="shortcut icon" href="https://tinhocdct.xyz/favicon.ico">
<link rel="canonical" href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-8-input-va-output-tiep-100.html">
<link rel="alternate" href="https://tinhocdct.xyz/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tin-tuc/" title="Tin Tức - TIN TỨC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/thong-cao-bao-chi/" title="Tin Tức - THÔNG BÁO" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/ban-tin-noi-bo/" title="Tin Tức - BẢN TIN NỘI BỘ" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tin-cong-nghe/" title="Tin Tức - CÔNG NGHỆ - KHOA HỌC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/thu-thuat/" title="Tin Tức - THỦ THUẬT" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tien-ich/" title="Tin Tức - TIỆN ÍCH" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/ai/" title="Tin Tức - AI" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/khoa-hoc/" title="Tin Tức - KHOA HỌC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-chuyen-mon/" title="Tin Tức - HOẠT ĐỘNG CHUYÊN MÔN" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-cong-doan/" title="Tin Tức - CÔNG ĐOÀN" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-doan-the/" title="Tin Tức - ĐOÀN THỂ" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tuyen-sinh-huong-nghiep/" title="Tin Tức - TUYỂN SINH - HƯỚNG NGHIỆP" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tuyen-sinh-2025/" title="Tin Tức - TUYỂN SINH" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/huong-nghiep/" title="Tin Tức - HƯỚNG NGHIỆP" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/lich-cong-tac-tuan/" title="Tin Tức - Lịch công tác tuần" type="application/rss+xml">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/jquery/jquery.min.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/language/vi.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/global.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/news.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/main.js">
<link rel="preload" as="script" href="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1816161148536198">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/bootstrap.min.js">
<link rel="StyleSheet" href="https://tinhocdct.xyz/assets/css/font-awesome.min.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/bootstrap.non-responsive.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/style.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/news.css">
<style>
	body{background: #fff;}
</style>
	</head>
	<body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG</h2>
		<p class="pull-right"><a title="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG" href="https://tinhocdct.xyz/">https://tinhocdct.xyz</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>&#91;C++ Cơ bản&#93; Phần 8&#x3A; Input và Output &#40;tiếp&#41;</h1>
		<ul class="list-inline">
			<li>Thứ tư - 07/04/2021 11:34</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
			Trong phần trước, chúng ta đã nói về các đọc và viết ra màn hình console. Bài viết này sẽ nói về việc đọc và viết từ file.Cảnh báo: Bài viết này sẽ có rất nhiều nội dung mới, chưa được giải thích về trước, và sẽ được giải thích trong các phần về sau.
		</div>
				<div class="imghome">
			<img alt="&#91;C++ Cơ bản&#93; Phần 8&#x3A; Input và Output &#40;tiếp&#41;" src="https://tinhocdct.xyz/uploads/news/2021_04/cpp-co-ban-1-3.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<h1 id="file-path"><span style="font-size:14px;"><em>Phần trước:&nbsp;<a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-7-input-output-99.html" target="_blank">&#91;C++ Cơ bản&#93; Phần 7: Input - Output</a></em></span><br  />
File path</h1>

<p>Mỗi file và folder đều có một địa chỉ xác định, địa chỉ này được biểu diễn bằng một string (xâu ký tự), được gọi là đường dẫn (path). Đường dẫn bao gồm tên các folder dẫn tới file, phân cách nhau bằng dấu gạch chéo (<code>/</code>&nbsp;hoặc&nbsp;<code>\\</code>, tùy thuộc vào hệ điều hành).</p>

<p>Đường dẫn có hai dạng: Đường dẫn tuyệt đối (absolute) và tương đối (relative)</p>

<ul>
	<li>Đường dẫn tuyệt đối sẽ chỉ định tới một địa chỉ cố định. Ví dụ:&nbsp;<code>C:\\user\\docs\\Letter.txt</code>&nbsp;sẽ chỉ định tới file&nbsp;<code>Letter.text</code>, nằm trong thư mục&nbsp;<code>docs</code>&nbsp;của thư mục&nbsp;<code>user</code>, trong ổ&nbsp;<code>C</code>. Đường dẫn tuyệt đối bắt buộc phải có tên của ổ đĩa hoặc thư mục gốc (root).</li>
	<li>Đường dẫn tương đối sẽ chỉ định tới một địa chỉ tương đối so với file của chương trình. Ví dụ: Chương trình được đặt trong thư mục&nbsp;<code>C:\\Program Files\\My Program</code>. Khi đó thay vì sử dụng đường dẫn tuyệt đối để truy cập vào file ở địa chỉ&nbsp;<code>C:\\Program Files\\My Program\\Stuff\\xyz.txt</code>, ta chỉ sử dụng đường dẫn tương đối&nbsp;<code>Stuff\\xyz.txt</code>.</li>
</ul>

<p>Bản thân tên của một file cũng là đường dẫn tương đối của nó, so với thư mục chứa nó.</p>

<p>Khi ta chạy một dự án C++ trên CodeBlocks, đường dẫn tương đối được so với thư mục của dự án, thay vì là thư mục của file chương trình. Tuy nhiên khi ta chạy đơn lẻ file chương trình sau khi build ra mà không thông qua CodeBlocks, đường dẫn tương đối sẽ là so với thư mục chứa file chương trình như mặc định.</p>

<h1 id="thư-viện-fstream">Thư viện&nbsp;<code>fstream</code></h1>

<p>C++ cung cấp thư viện&nbsp;<code>fstream</code>&nbsp;dành cho việc nhập - xuất với file. Thư viện này định nghĩa ba class mới:</p>

<table>
	<tbody>
		<tr>
			<th>Class</th>
			<th>Chức năng</th>
		</tr>
		<tr>
			<td><code>ofstream</code></td>
			<td>Class biểu diễn output file stream và được sử dụng để tạo mới và viết ra file.</td>
		</tr>
		<tr>
			<td><code>ifstream</code></td>
			<td>Class biểu diễn input file stream và được sử dụng để đọc thông tin từ file.</td>
		</tr>
		<tr>
			<td><code>fstream</code></td>
			<td>Class biểu diễn các stream liên quan tới file nói chung, và bao gồm khả năng của cả&nbsp;<code>ofstream</code>&nbsp;và&nbsp;<code>ifstream</code>.</td>
		</tr>
	</tbody>
</table>

<p>Để xử lý file với C++, ta cần phải khai báo sử dụng hai thư viện&nbsp;<code>iostream</code>&nbsp;và&nbsp;<code>fstream</code></p>

<figure>
<pre>
<code data-lang="c++">\#include &lt;iostream&gt;
\#include &lt;fstream&gt;</code></pre>
</figure>

<h1 id="khai-báo-file"><br  />
<br  />
<br  />
<br  />
<br  />
Khai báo file</h1>

<p>Trước khi đọc hoặc viết một file bất kì, ta phải khai báo file stream dành cho file đó trong chương trình.</p>

<p>Việc khai báo file stream mới cũng giống như khai báo một biến mới - ở đây ta đặt tên biến file stream là&nbsp;<code>myFile</code>:</p>

<figure>
<pre>
<code data-lang="c++">fstream myFile;</code></pre>
</figure>

<p><br  />
<br  />
<br  />
<br  />
Sau đó, ta cần gọi lệnh&nbsp;<code>open()</code>&nbsp;của đối tượng&nbsp;<code>fstream</code>, để mở file và hướng file stream vào file mục tiêu.</p>

<p><strong>Chú ý:</strong>&nbsp;Các kiến thức về hàm và lệnh của đối tượng, cũng như các nội dung về lập trình hướng đối tượng nói chung, sẽ được giải thích trong các bài viết sau. Hiện tại, hãy tạm thời làm theo chỉ dẫn của bài viết.</p>

<p>Cú pháp của lệnh&nbsp;<code>open()</code>&nbsp;là:</p>

<figure>
<pre>
<code data-lang="c++">stream_variable.open(const char \*filename, ios::openmode mode);</code></pre>
</figure>
&nbsp;

<ul>
	<li><code>filename</code>&nbsp;là file path của file mục tiêu. Biến này có kiểu dữ liệu&nbsp;<code>const char \*</code>, là kiểu dữ liệu con trỏ cố định chỉ tới một biến kiểu&nbsp;<code>char</code>. Phần nội dung về biến cố định (hằng số) và con trỏ sẽ được nói tới một bài viết khác. Hiện tại, ta sẽ sử dụng địa chỉ của file nằm trong ngoặc kép (<code>&quot;&quot;</code>) như là địa chỉ của file.</li>
	<li><code>mode</code>&nbsp;là một giá trị kiểu bitmask không bắt buộc. Nó cho phép người dùng cài đặt chế độ mở file. Các giá trị có thể thiết lập là:</li>
</ul>

<table>
	<tbody>
		<tr>
			<th>Mode Flag</th>
			<th>Chức năng</th>
		</tr>
		<tr>
			<td><code>ios::app</code></td>
			<td>Chế độ nối tiếp. Khi mở một file để viết dữ liệu vào, tất cả phần nội dung mới sẽ được viết vào cuối file.</td>
		</tr>
		<tr>
			<td><code>ios::ate</code></td>
			<td>Mở một file ra để viết nội dung mới vào, và di chuyển con trỏ viết tới cuối file này.</td>
		</tr>
		<tr>
			<td><code>ios::in</code></td>
			<td>Mở file ra để đọc</td>
		</tr>
		<tr>
			<td><code>ios::out</code></td>
			<td>Mở file ra để viết</td>
		</tr>
		<tr>
		</tr>
		<tr>
			<td><code>ios::trunc</code></td>
			<td>Mở file ra để viết. Nếu như file mục tiêu đã tồn tại trước đó và ta cần viết nội dung vào file, nó sẽ bị xóa sạch nội dung cũ trước khi viết nội dung mới.</td>
		</tr>
	</tbody>
</table>

<p><code>mode</code>&nbsp;là một giá trị kiểu bitmask, do đó ta có thể sử dụng toán tử OR (<code>|</code>) để áp dụng nhiều mode vào cùng một lúc.</p>

<p><strong>Chú ý:</strong>&nbsp;OR và các toán tử khác liên quan tới toán học và xử lý bit sẽ được đề cập ở các bài viết sau.</p>

<p>Ví dụ: Mở file&nbsp;<code>myFile.txt</code>&nbsp;ra để viết bằng luồng&nbsp;<code>myFile</code>&nbsp;ở trên, và nếu file đõ đã tồn tại thì xóa nội dung của nó đi:</p>

<figure>
<pre>
<code data-lang="c++">myFile.open(&quot;myFile.txt&quot;, ios::out | ios:: trunc);</code></pre>
</figure>

<p><br  />
<br  />
<br  />
<br  />
Một ví dụ khác: Mở file&nbsp;<code>myFile.txt</code>&nbsp;ra để đọc và viết cùng lúc:</p>

<figure>
<pre>
<code data-lang="c++">myFile.open(&quot;myFile.txt&quot;, ios::in | ios::out);</code></pre>
</figure>

<p><br  />
<br  />
<br  />
<br  />
Cả hai đoạn code này sẽ mở ra file&nbsp;<code>myFile.txt</code>&nbsp;ở trong thư mục của chương trình, do đường dẫn được sử dụng là đường dẫn tương đối.</p>

<p>Chú ý:&nbsp;<code>mode</code>&nbsp;là giá trị không bắt buộc phải có.</p>

<ul>
	<li>Đối với các biến dạng&nbsp;<code>fstream</code>,&nbsp;<code>mode</code>&nbsp;được đặt mặc định bằng&nbsp;<code>ios::in | ios::out</code>.</li>
	<li>Đối với các biến dạng&nbsp;<code>ofstream</code>,&nbsp;<code>mode</code>&nbsp;được đặt mặc định là&nbsp;<code>ios::out</code>.</li>
	<li>Đối với các biến dạng&nbsp;<code>ifstream</code>,&nbsp;<code>mode</code>&nbsp;được đặt mặc định bằng&nbsp;<code>ios::in</code>.</li>
</ul>

<p>Do đó nếu không có nhu cầu đặc biệt, ta có thể bỏ qua giá trị của&nbsp;<code>mode</code>.</p>

<p>Ví dụ: Mở file&nbsp;<code>myFile.txt</code>&nbsp;để đọc bằng input file stream&nbsp;<code>myFile</code></p>

<figure>
<pre>
<code data-lang="c++">ifstream myFile;
myFile.open(&quot;myFile.txt&quot;);</code></pre>
</figure>

<p><br  />
<br  />
<br  />
<br  />
Nếu như luồng đang được chỉ địng tới một file nào đó mà chưa được đóng lại, lệnh này sẽ thất bại, và việc nhập/xuất sẽ bị sai.</p>

<h1 id="đọc-và-viết-vào-file">Đọc và viết vào file</h1>

<p>Để đọc và viết vào file, ta chỉ cần sử dụng toán tử&nbsp;<code>&gt;&gt;</code>&nbsp;và&nbsp;<code>&lt;&lt;</code>&nbsp;lên các luồng, tương tự như với&nbsp;<code>cin</code>&nbsp;và&nbsp;<code>cout</code>.</p>

<p>Ví dụ: Đọc một số từ file&nbsp;<code>INPUT.TXT</code>, và in số đó ra file&nbsp;<code>OUTPUT.TXT</code></p>

<figure>
<pre>
<code data-lang="c++">\#include &lt;iostream&gt;
\#include &lt;fstream&gt;

using namespace std;

int main()
{
ifstream input; input.open(&quot;INPUT.TXT&quot;);
ofstream output; output.open(&quot;OUTPUT.TXT&quot;);
int x; input &gt;&gt; x;
output &lt;&lt; x;
input.close(); output.close(); // Đóng file
return 0;
}</code></pre>
</figure>

<h1 id="đóng-file"><br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
Đóng file</h1>

<p>Khi một chương trình C++ kết thúc, nó sẽ tự động giải phóng buffer và các vùng bộ nhớ tạm thời, và đóng tất cả các file đang mở lại. Tuy nhiên, việc đóng các file lại trước khi kết thúc chương trình cũng là một việc nên làm, tránh sự ảnh hưởng từ bên ngoài chương trình.</p>

<p>Như trong ví dụ trên, để đóng một file lại, ta sử dụng cú pháp</p>

<figure>
<pre>
<code data-lang="c++">stream_variable.close();</code></pre>
</figure>

<h1 id="di-chuyển-con-trỏ-đọcviết"><br  />
<br  />
<br  />
<br  />
Di chuyển con trỏ đọc/viết</h1>

<p>Class&nbsp;<code>istream</code>&nbsp;cung cấp lệnh&nbsp;<code>seekg</code>&nbsp;(“seek get”) để di chuyển con trỏ đọc luồng input. Cú pháp của lệnh này là</p>

<pre>
<code>luồng.seekg(long long int pos, ios_base::seekdir way);
</code></pre>

<ul>
	<li><code>pos</code>&nbsp;là số byte cần di chuyển.</li>
	<li><code>way</code>&nbsp;là một giá trị không bắt buộc, chỉ định địa điểm bắt đầu di chuyển. Giá trị phù hợp của way là&nbsp;<code>ios::beg</code>&nbsp;(từ điểm bắt đầu của luồng/file, mặc định),&nbsp;<code>ios::cur</code>&nbsp;(từ vị trí hiện tại) và&nbsp;<code>ios::end</code>&nbsp;(từ điểm kết thúc của luồng/file).</li>
</ul>

<p>Ví dụ:</p>

<figure>
<pre>
<code data-lang="c++">// Di chuyển tới byte thứ n của fileObject
fileObject.seekg( n );

// Di chuyển thêm n byte nữa trong fileObject
fileObject.seekg( n, ios::cur );

// Di chuyển tới byte thứ n từ cuối lên
fileObject.seekg(- n, ios::end );

// Di chuyển tới cuối
fileObject.seekg( 0, ios::end );</code></pre>
</figure>

<p><br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
Class&nbsp;<code>ofstream</code>&nbsp;cũng cung cấp lệnh&nbsp;<code>seekp</code>&nbsp;(“seek put”) với cú pháp tương tự.</p>

<h1 id="freopen"><code>freopen</code></h1>

<p><code>freopen</code>&nbsp;được sử dụng nhiều bởi giới lập trình thi đấu, vì trong lập trình thi đấu thì ta không có nhu cầu phải đọc hoặc in với nhiều file một lúc. Sau khi áp dụng&nbsp;<code>freopen</code>, ta có thể sử dụng&nbsp;<code>cin</code>&nbsp;và&nbsp;<code>cout</code>&nbsp;để đọc viết trên file - điều này rất có ích khi ta muốn copy đoạn code mẫu từ nơi này tới nơi khác, mà không phải lo về tên biến luồng dữ liệu.</p>

<p><code>freopen</code>&nbsp;là một lệnh được định nghĩa trong thư viện&nbsp;<code>stdio.h</code>&nbsp;của C. Nó cho phép người dùng chuyển hướng của luồng dữ liệu&nbsp;<code>input</code>&nbsp;hoặc&nbsp;<code>output</code>&nbsp;sang một file khác, nhằm đọc và viết trên file đó.</p>

<p>Cú pháp của&nbsp;<code>freopen</code>&nbsp;là:</p>

<figure>
<pre>
<code data-lang="c++">freopen(const char \*filename, const char \*mode, FILE \*stream);</code></pre>
</figure>
<br  />
<br  />
<br  />
<br  />
&nbsp;
<ul>
	<li><code>filename</code>&nbsp;là địa chỉ của file mục tiêu. Giá trị này thuộc kiểu&nbsp;<code>const char \*</code>, giống như&nbsp;<code>filename</code>&nbsp;trong hàm&nbsp;<code>open</code>&nbsp;của&nbsp;<code>fstream</code>.</li>
	<li><code>mode</code>&nbsp;là chế độ mở file. Các giá trị phù hợp ở đây là:</li>
</ul>

<table>
	<tbody>
		<tr>
			<th>Giá trị</th>
			<th>Ý nghĩa</th>
		</tr>
		<tr>
			<td><code>&quot;r&quot;</code></td>
			<td>Mở file để đọc. File cần mở phải tồn tại trước đó.</td>
		</tr>
		<tr>
			<td><code>&quot;w&quot;</code></td>
			<td>Mở file để viết. Nếu file đã tồn tại trước đó, thì nội dung của nó sẽ bị xóa trắng trước khi mở. Nếu không, nó sẽ được tạo mới.</td>
		</tr>
		<tr>
			<td><code>&quot;a&quot;</code></td>
			<td>Mở file để viết. Nếu file đã tồn tại trước đó, nội dung mới sẽ được viết tiếp vào cuối file. Nếu không, nó sẽ được tạo mới.</td>
		</tr>
		<tr>
			<td><code>&quot;r\+&quot;</code></td>
			<td>Mở một file để vừa đọc và viết. File phải tồn tại trước đó.</td>
		</tr>
		<tr>
			<td><code>&quot;w\+&quot;</code></td>
			<td>Tạo một file rỗng mới để vừa đọc vừa viết.</td>
		</tr>
		<tr>
			<td><code>&quot;a\+&quot;</code></td>
			<td>Mở một file để vừa đọc và viết. Nội dung mới sẽ được nối vào cuối file cũ.</td>
		</tr>
	</tbody>
</table>

<ul>
	<li><code>stream</code>&nbsp;là luồng dữ liệu cần chuyển. Giá trị ở đây có thể là&nbsp;<code>stdin</code>&nbsp;(luồng vào chuẩn) hoặc&nbsp;<code>stdout</code>&nbsp;(luồng ra chuẩn), hoặc các luồng vào và luồng ra khác.</li>
</ul>

<p>Sau khi gọi lệnh này với&nbsp;<code>stdin</code>&nbsp;hoặc&nbsp;<code>stdout</code>, ta có thể đọc và viết với file bằng luồng&nbsp;<code>cin</code>&nbsp;và&nbsp;<code>cout</code>&nbsp;sẵn có.</p>

<p>Ví dụ: Khi áp dụng vào chương trình ở trên:</p>

<figure>
<pre>
<code data-lang="c++">\#include &lt;iostream&gt;
\#include &lt;stdio.h&gt;

using namespace std;

int main()
{
freopen(&quot;INPUT.TXT&quot;, &quot;r&quot;, stdin);
freopen(&quot;OUTPUT.TXT&quot;, &quot;w&quot;, stdout);
int x;
cin &gt;&gt; x;
cout &lt;&lt; x;
return 0;
}</code></pre>
</figure>

<p><br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<em>​​​​​​​Phần sau:&nbsp;<a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-9-toan-tu-102.html" target="_blank">&#91;C++ Cơ bản&#93; Phần 9: Toán tử</a></em></p>
		</div>
				<div id="author">
						<p>
				<strong>Tác giả bài viết:</strong>
				Thanh Sơn
			</p>
						<p>
				<strong>Nguồn tin:</strong>
				cowboycoder.tech
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-8-input-va-output-tiep-100.html" title="&#91;C++ Cơ bản&#93; Phần 8&#x3A; Input và Output &#40;tiếp&#41;">https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-8-input-va-output-tiep-100.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG
		</div>
		<div id="contact">
			<a href="mailto:webmaster@tinhocdct.xyz">webmaster@tinhocdct.xyz</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://tinhocdct.xyz/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<script src="https://tinhocdct.xyz/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_is_recaptcha=0;</script>
<script src="https://tinhocdct.xyz/assets/js/language/vi.js"></script>
<script src="https://tinhocdct.xyz/assets/js/global.js"></script>
<script src="https://tinhocdct.xyz/themes/default/js/news.js"></script>
<script src="https://tinhocdct.xyz/themes/default/js/main.js"></script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1816161148536198"
     crossorigin="anonymous"></script>
<script src="https://tinhocdct.xyz/themes/default/js/bootstrap.min.js"></script>
</body>
</html>