⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page_148.html

📁 Programming and Problem Solving with C++
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">	<html>		<head>			<title>page_148</title>			<link rel="stylesheet" href="reset.css" type="text/css" media="all">			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		</head>		<body>		<table summary="top nav" border="0" width="100%">			<tr>				<td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_147.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_148</strong></td>				<td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_149.html">next page&nbsp;&gt;</a></td>			</tr>					<tr>				<td align="left" colspan="3" style="background: #ffffff; padding: 20px;">    <table border="0" width="100%" cellpadding="0"><tr><td align="center">  <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td>  <td align="right"></td>  </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 148</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">ables in your program. But some computer systems do not use this format for file names on disk. For example, many systems include a dot in file names.) Similarly, the second function call associates the stream variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">outMPG</font><font face="Times New Roman, Times, Serif" size="3"> with the disk file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">outmpg.dat</font><font face="Times New Roman, Times, Serif" size="3">. Associating a program's name for a file (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">outMPG</font><font face="Times New Roman, Times, Serif" size="3">) with the actual name for the file (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">outmpg.dat</font><font face="Times New Roman, Times, Serif" size="3">) is much the same as associating a program's name for the standard output device (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">cout</font><font face="Times New Roman, Times, Serif" size="3">) with the actual device (the screen).</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">The next thing the open function does depends on whether the file is an input file or an output file. With an input file, the open function sets the file reading marker to the first piece of data in the file. (Each input file has its own reading marker.)</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">With an output file, the open function checks to see whether the file already exists. If the file doesn't exist, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">open</font><font face="Times New Roman, Times, Serif" size="3"> creates a new, empty file for you. If the file does exist, open erases the old contents of the file. Then the writing marker is set at the beginning of the empty file (see Figure 4-3). As output proceeds, each successive output operation advances the writing marker to add data to the end of the file.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">Because the reason for opening files is to <i>prepare</i> the files for reading or writing, you must open the files before using any input or output statements that refer to the files. Ina program, it's a good idea to open files right away to be sure that the files are prepared before the program attempts any file I/O.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">聽聽.<br />聽聽.<br />聽聽.<br /><br />int聽main()<br />{<br />聽聽聽聽聽.<br />聽聽聽聽聽.<br />聽聽聽聽聽.}Declarations<br />聽聽聽聽聽<br />聽聽//聽Open聽the聽files<br /><br />聽聽inMPG.open("inmpg.dat");<br />聽聽outMPG.open("outmpg.dat");<br /><br />聽聽聽聽聽.<br />聽聽聽聽聽.<br />聽聽聽聽聽.<br /><br />}</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">Specifying Files in Input/Output Statements</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">There is just one more thing we have to do in order to use files. As we said earlier, all </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">istream</font><font face="Times New Roman, Times, Serif" size="3"> operations are also valid for the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ifstream</font><font face="Times New Roman, Times, Serif" size="3"> type, and all </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ostream</font><font face="Times New Roman, Times, Serif" size="3"> operations are valid for the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ofstream</font><font face="Times New Roman, Times, Serif" size="3"> type. So, to read from or write to a file, all we need to do in our input and output statements is substitute the appropriate file stream variable for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">cin</font><font face="Times New Roman, Times, Serif" size="3"> or </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">cout</font><font face="Times New Roman, Times, Serif" size="3">. In our Mileage program, we would use a statement like</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>  </td>			</tr>				<tr>				<td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_147.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_148</strong></td>				<td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_149.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -