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

📄 page_675.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_675</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_674.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_675</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_676.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 675</font></td></tr></table><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td height="12"></td></tr><tr><td><table cellspacing="0" width="548" cellpadding="7"><tr><td valign="bottom"><font face="Times New Roman, Times, Serif" size="2"><i>Statement</i></font></td><td valign="bottom"><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="center"><font face="Times New Roman, Times, Serif" size="2"><i>Skips Leading Whitespace?</i></font></td></tr></table></td><td valign="bottom"><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="center"><font face="Times New Roman, Times, Serif" size="2"><i>Stops Reading When?</i></font></td></tr></table></td></tr><tr><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">cin &gt;&gt; inputStr;</font></td><td valign="top"><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="center"><font face="Times New Roman, Times, Serif" size="2">Yes</font></td></tr></table></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">At the first trailing whitespace character (which is <i>not</i> consumed)</font></td></tr><tr><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">cin.get(inputStr, 21);</font></td><td valign="top"><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="center"><font face="Times New Roman, Times, Serif" size="2">No</font></td></tr></table></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">When either 20 characters are read or '\n' is encountered (which is <i>not</i> consumed)</font></td></tr></table></td></tr></table><br /><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"><i>Run-Time Input of File Names</i></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">Until now, our programs that have read from input files and written to output files have included code similar to this:</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">ifstream聽inFile;聽聽聽聽//聽Input聽file聽to聽be聽analyzed<br /><br />inFile.open("datafile.dat");<br />if聽(聽!inFile聽)<br />{<br />聽聽聽聽cout聽&lt;&lt;聽"**聽Can't聽open聽input聽file聽**"聽&lt;&lt;聽endl;<br />聽聽聽聽return聽1;<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">The open function associated with the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ifstream</font><font face="Times New Roman, Times, Serif" size="3"> data type requires a string parameter that specifies the name of the actual data file on disk. By using a string constant, as in the above example, the file name is fixed at compile time. That is, the program works only for this one particular disk 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">We often want to make a program more flexible by allowing the file name to be determined at <i>run time.</i> A common technique is to prompt the user for the name of the file, read the user's response into a string variable, and pass the string variable as a parameter to the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">open</font><font face="Times New Roman, Times, Serif" size="3"> function. The following code fragment, which is another example of the use of the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">get</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ignore</font><font face="Times New Roman, Times, Serif" size="3"> functions, demonstrates the run-time input of a file name.</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">ifstream聽inFile;聽聽聽聽聽聽聽聽聽//聽Input聽file聽to聽be聽analyzed<br />char聽聽聽聽聽fileName[51];聽聽聽//聽Max.聽50聽characters聽plus聽'\0'<br /><br />cout聽&lt;&lt;聽"Enter聽the聽input聽file聽name:聽";<br />cin.get(fileName,聽51);聽聽聽聽聽聽聽聽聽聽聽//聽Read聽at聽most聽50聽characters<br />cin.ignore(100,聽'\n');聽聽聽聽聽聽聽聽聽聽聽//聽Skip聽rest聽of聽input聽line<br /><br />inFile.open(fileName);<br />if聽(聽!inFile聽)<br />{</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_674.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_675</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_676.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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