VC(C)操作文件之-文件的打开(fopen函数)

							<h2 id="t_7065a9de01010gz5" class="titName SG_txta">VC(C)操作文件之-文件的打开(fopen函数)</h2>
		
				<span class="time SG_txtc">(2011-12-21 16:06:23)</span><div class="turnBoxzz"><a href="javascript:;" class="SG_aBtn SG_aBtn_ico SG_turn" action-type="reblog" action-data="{srcBlog:1, blogId:'7065a9de01010gz5'}"><cite><img class="SG_icon SG_icon111" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" align="absmiddle">转载<em class="arrow">▼</em></cite></a></div>		</div>
	<div class="articalTag" id="sina_keyword_ad_area">
		<table>
			<tbody><tr>
				<td class="blog_tag">
				<script>
				var $tag='杂谈';
				var $tag_code='8559756a5f989c8d2db543e3942ddb19';
				var $r_quote_bligid='7065a9de01010gz5';
				var $worldcup='0';
				var $worldcupball='0';
				</script>
										<span class="SG_txtb">标签:</span>
																			<h3><a href="http://search.sina.com.cn/?c=blog&amp;q=%D4%D3%CC%B8&amp;by=tag" target="_blank">杂谈</a></h3>
															</td>
				<td class="blog_class">
										<span class="SG_txtb">分类:</span>
					<a target="_blank" href="http://blog.sina.com.cn/s/articlelist_1885710814_1_1.html">VC 技术</a>
									</td>
			</tr>
		</tbody></table>
	</div>
					<!-- 正文开始 -->
	<div id="sina_keyword_ad_area2" class="articalContent   ">
		<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px;">
文件的打开(fopen函数)

fopen函数用来打开一个文件,其调用的一般形式为: 
文件指针名=fopen(文件名,使用文件方式); 
其中, 
“文件指针名”必须是被说明为FILE 类型的指针变量; 
“文件名”是被打开文件的文件名; 
“使用文件方式”是指文件的类型和操作要求。 
“文件名”是字符串常量或字符串数组。 
例如: 
FILE *fp; 
fp=("file a","r"); 
其意义是在当前目录下打开文件file a,只允许进行“读”操作,并使fp指向该文件。
又如: 
FILE *fphzk 
fphzk=("c:\\hzk16","rb") 
其意义是打开C驱动器磁盘的根目录下的文件hzk16,这是一个二进制文件,只允许按二进制方式进行读操作。两个反斜线“\\ ”中的第一个表示转义字符,第二个表示根目录。 

使用文件的方式共有12种,下面给出了它们的符号和意义。 
文件使用方式 意义 
“rt” 只读打开一个文本文件,只允许读数据 
“wt” 只写打开或建立一个文本文件,只允许写数据 
“at” 追加打开一个文本文件,并在文件末尾写数据 
“rb” 只读打开一个二进制文件,只允许读数据 
“wb” 只写打开或建立一个二进制文件,只允许写数据 
“ab” 追加打开一个二进制文件,并在文件末尾写数据 
“rt+” 读写打开一个文本文件,允许读和写 
“wt+” 读写打开或建立一个文本文件,允许读写 
“at+” 读写打开一个文本文件,允许读,或在文件末追加数据 
“rb+” 读写打开一个二进制文件,允许读和写 
“wb+” 读写打开或建立一个二进制文件,允许读和写 
“ab+” 读写打开一个二进制文件,允许读,或在文件末追加数据 

对于文件使用方式有以下几点说明: 
1) 文件使用方式由r,w,a,t,b,+六个字符拼成,各字符的含义是: 
r(read): 读 
w(write): 写 
a(append): 追加 
t(text): 文本文件,可省略不写 
b(banary): 二进制文件 
+: 读和写 
2) 凡用“r”打开一个文件时,该文件必须已经存在,且只能从该文件读出。
3) 用“w”打开的文件只能向该文件写入。若打开的文件不存在,则以指定的文件名建立该文件,若打开的文件已经存在,则将该文件删去,重建一个新文件。 

若要向一个已存在的文件追加新的信息,只能用“a”方式打开文件。但此时该文件必须是存在的,否则将会出错。 

在打开一个文件时,如果出错,fopen将返回一个空指针值NULL。在程序中可以用这一信息来判别是否完成打开文件的工作,并作相应的处理。因此常用以下程序段打开文件: 

  1. if((fp=fopen(“c:\hzk16”,“rb”)==NULL) 



    printf("\nerror on open c:\hzk16 file!"); 

    getch(); 

    exit(1); 





    这段程序的意义是,如果返回的指针为空,表示不能打开C盘根目录下的hzk16文件,则给出提示信息“error on open c:
    hzk16
    file!”,下一行getch()的功能是从键盘输入一个字符,但不在屏幕上显示。在这里,该行的作用是等待,只有当用户从键盘敲任一键时,程序才继续执行,因此用户可利用这个等待时间阅读出错提示。敲键后执行exit(1)退出程序。 

7) 把一个文本文件读入内存时,要将ASCII码转换成二进制码,而把文件以文本方式写入磁盘时,也要把二进制码转换成ASCII码,因此文本文件的读写要花费较多的转换时间。对二进制文件的读写不存在这种转换。 

8) 标准输入文件(键盘),标准输出文件(显示器),标准出错输出(出错信息)是由系统打开的,可直接使用。 转载:

http://www.cppblog.com/wrhwww/archive/2009/03/13/76430.html


1

扫描二维码关注公众号,回复: 6430363 查看本文章

0

        </div>
        <div class="clearit"></div>
	</div>
	<div class="articalInfo">
		<!-- 分享到微博 {$t_blog} -->
		<div class="IL">
			阅读<span id="r_7065a9de01010gz5" class="SG_txtb">(1602)</span><em class="SG_txtb">┊</em> 
			<a href="#commonComment">评论</a> <span id="c_7065a9de01010gz5" class="SG_txtb">(0)</span><em class="SG_txtb">┊</em>				<a href="javascript:;" onclick="$articleManage('7065a9de01010gz5',5);return false;">收藏</a><span id="f_7065a9de01010gz5" class="SG_txtb">(0)</span>
			<em class="SG_txtb">┊</em><a href="#" id="quote_set_sign" onclick="return false ;">转载</a><a href="#" id="z_7065a9de01010gz5" onclick="return false ;" class="zznum">(0)</a>				<span id="fn_VC(C)操作文件之-文件的打开(fopen函数)" class="SG_txtb"></span><em class="SG_txtb">┊</em>
			<a onclick="return false;" href="javascript:;"><cite id="d1_digg_7065a9de01010gz5">喜欢</cite></a><a id="d1_digg_down_7065a9de01010gz5" href="javascript:;"><b>▼</b></a>
								<em class="SG_txtb">┊</em><a href="http://blog.sina.com.cn/main_v5/ria/print.html?blog_id=blog_7065a9de01010gz5" target="_blank">打印</a><em class="SG_txtb">┊</em><a id="q_7065a9de01010gz5" onclick="report('7065a9de01010gz5');return false;" href="#">举报</a>
										</div>
		<div class="IR">
			<table>
				<tbody><tr><!--
										<th class="SG_txtb" scope="row">已投稿到:</th>
					<td>
						<div class="IR_list">
							<span><img class="SG_icon SG_icon36" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" title="排行榜" align="absmiddle" /> <a href="http://blog.sina.com.cn/lm/114/117/day.html" class="SG_linkb" target="_blank">排行榜</a></span>							</div>
					</td>
				-->
				</tr>
								</tbody></table>
		</div>
	</div>
	<div class="clearit"></div>
	<div class="blogzz_zzlist borderc" id="blog_quote" style="display:none"><h3><a href="#" onclick="return false" title="关闭" id="ql_close7065a9de01010gz5" class="blogzz_closepic SG_floatR"></a>转载列表:</h3>                <ul class="ul_zzlist" id="ql_content7065a9de01010gz5">                </ul>				<ul style="display:none"><li id="ql_tip7065a9de01010gz5"></li></ul>                <div class="SG_clearB"></div>                <div class="blogzz_btn">					<a id="btnArticleQuote7065a9de01010gz5" href="#" onclick="scope.article_quote &amp;&amp; scope.article_quote.check('7065a9de01010gz5');return false;" class="SG_aBtn SG_aBtn_ico SG_turn"><cite><img class="SG_icon SG_icon111" id="quoteList_quote7065a9de01010gz5" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" align="absmiddle">转载</cite></a>					<p id="quoteDescription7065a9de01010gz5" class="SG_turntxt" style="display: none;">转载是分享博文的一种常用方式...</p>				</div>				<div id="ql_page7065a9de01010gz5" class="blogzz_paged"></div>				<div class="clearit"></div></div>
	<div class="articalfrontback SG_j_linedot1 clearfix" id="new_nextprev_7065a9de01010gz5">
						<div><span class="SG_txtb">前一篇:</span><a href="http://blog.sina.com.cn/s/blog_7065a9de01010gyr.html">VC++&nbsp;什么是回调函数?</a></div>
									<div><span class="SG_txtb">后一篇:</span><a href="http://blog.sina.com.cn/s/blog_7065a9de01010kgz.html">Makefile的使用</a></div>
				</div>
	<div class="clearit"></div>
						
	<div id="loginFollow"></div>
			<div class="allComm">
		<div class="allCommTit">
			<div class="SG_floatL">
			    <strong>评论</strong>
			    <span id="commAd_1" style="display: inline-block;">
			        <span style="margin-left:15px; width:220px; display:inline-block;"><a target="_blank" href="http://blog.sina.com.cn/lm/8/2009/0325/105340.html">重要提示:警惕虚假中奖信息</a></span>
			    </span>
			</div>
			<div class="SG_floatR"><a class="CP_a_fuc" href="#post">[<cite>发评论</cite>]</a></div>
		</div>
		<ul id="article_comment_list" class="SG_cmp_revert"><li><div class="noCommdate"><span class="SG_txtb">做第一个评论者吧! <img class="SG_icon SG_icon134" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="18" height="18" title="" align="absmiddle"><a href="#post">抢沙发&gt;&gt;</a></span></div></li></ul>
		<div class="clearit"></div>
		<div class="myCommPages SG_j_linedot1" style="display: none;">
			<div class="SG_page" id="commentPaging" style="display: none; text-align: center;"><a href="javascript:void(0);" style="">点击加载更多</a></div>
			<div class="clearit"></div>
		</div>
		<a name="post"></a>
		<div class="writeComm">
			<div class="allCommTit">
				<div class="SG_floatL">
				    <strong>发评论</strong>
				    <span></span>
				</div>
				<div class="SG_floatR"></div>
			</div>
			<div class="wrCommTit">
				<div class="SG_floatL" id="commentNick" style="display:none;"></div>
			</div>
			<div class="formTextarea">
				<div style="float:left;" id="commonComment">
				<iframe id="postCommentIframe" frameborder="0" style="border:1px solid #C7C7C7;
	height:158px;width:448px;maring-top:1px;background-color:white;" src="http://blog.sina.com.cn/main_v5/ria/blank2.html"></iframe>
				<textarea id="commentArea" tabindex="1" style="display:none;"></textarea>
				</div>
				<div id="mobileComment" style="float:left;display:none;">
					<textarea id="mbCommentTa" style="width:438px;height:150px;border:1px solid #C7C7C7;line-height:18px;padding:5px;"></textarea>
				</div>
				<div class="faceblk" id="faceWrap">
					<div id="smilesSortShow" class="faceline1">
					<div class="facestyle" id="recomm_1556608761075"><a href="#" key="302"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/302-25.gif" alt="小新小浪" title="小新小浪"></a><a href="#" key="308"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/308-25.gif" alt="炮炮兵" title="炮炮兵"></a><a href="#" key="315"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/315-25.gif" alt="张富贵" title="张富贵"></a><a href="#" key="316"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/316-25.gif" alt="旺狗" title="旺狗"></a><a href="#" key="331"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/331-25.gif" alt="悠嘻猴" title="悠嘻猴"></a><a href="#" key="351"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/351-25.gif" alt="酷巴熊" title="酷巴熊"></a></div><span class="SG_more"><a href="#">更多&gt;&gt;</a></span><div class="clearit"></div></div>
					<ul id="smilesRecommended" class="faceline01"><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0321EN00SIGT.gif" alt="就不买你" title="就不买你" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0320EN00SIGT.gif" alt="股市" title="股市" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0319EN00SIGT.gif" alt="发霉" title="发霉" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0318EN00SIGT.gif" alt="陈水边" title="陈水边" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0317EN00SIGT.gif" alt="裁员" title="裁员" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0316EN00SIGT.gif" alt="音乐" title="音乐" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0315EN00SIGT.gif" alt="贴你" title="贴你" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0314EN00SIGT.gif" alt="抢车位" title="抢车位" height="50" width="50"></a></li></ul>
				</div>
				<div class="clearit"></div>
			</div>
			<div class="formLogin">
				<div class="SG_floatL"> 
				<p id="commentlogin" style="display: block;"><span>登录名:</span><input type="text" style="width: 115px;" id="login_name" tabindex="2">   <span>密码:</span><input type="password" style="width: 115px;" id="login_pass" tabindex="3">   <a href="https://login.sina.com.cn/getpass.html" target="_blank">找回密码</a>   <a href="https://login.sina.com.cn/signup/signup.php?entry=blog&amp;src=blogicp&amp;srcuid=1885710814" target="_blank">注册</a>	<input type="checkbox" id="login_remember"><label for="login_remember" style="display:inline-block;" title="建议在网吧/公用电脑上取消该选项">记住登录状态</label></p><p id="commentloginM" style="display:none;"><span>昵&nbsp;&nbsp;&nbsp;称:</span><input type="text" style="width: 115px;" id="comment_anonyous" value="新浪网友" tabindex="2" disabled=""></p><p id="quote_comment_p"><!--<input type="checkbox" id="bb"> <label for="bb"><img height="18" align="absmiddle" width="18" title="" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" class="SG_icon SG_icon110">分享到微博 <img height="15" align="absmiddle" width="15" title="新" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" class="SG_icon SG_icon11"></label>&nbsp;&nbsp;&nbsp;--><input type="checkbox" id="cbCommentQuote"><label for="cbCommentQuote">评论并转载此博文</label><img class="SG_icon SG_icon11" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" title="新" align="absmiddle"></p>
				<p id="geetest-box"></p>
				</div>

				<span style="display: none; color: rgb(153, 153, 153); margin-left: 10px;" id="login_remember_caution"></span>

										<!--<div class="SG_floatR" id="anonymity_cont"><input type="checkbox" id="anonymity"/><label for="anonymity">匿名评论</label></div>-->
								</div>
			<div class="formBtn">
				<a href="javascript:;" onclick="return false;" class="SG_aBtn" tabindex="5"><cite id="postcommentid">发评论</cite></a>
				<p class="SG_txtc">以上网友发言只代表其个人观点,不代表新浪网的观点或立场。</p>
			</div>
		</div>
	</div>
			<div class="clearit"></div>
	
			<div class="articalfrontback articalfrontback2 clearfix">
					  <div class="SG_floatL"><span class="SG_txtb">&lt;&nbsp;前一篇</span><a href="http://blog.sina.com.cn/s/blog_7065a9de01010gyr.html">VC++&nbsp;什么是回调函数?</a></div>
								  <div class="SG_floatR"><span class="SG_txtb">后一篇&nbsp;&gt;</span><a href="http://blog.sina.com.cn/s/blog_7065a9de01010kgz.html">Makefile的使用</a></div>
				</div>
	<div class="clearit"></div>
			
</div>

猜你喜欢

转载自blog.csdn.net/wu694128/article/details/89710339