fseek不起作用,写操作定位无效
 2019.01.24    |      C/C++    |     AilsonJack    |     暂无评论    |     1464 views
By: Ailson Jack
Date: 2019-01-24
个人博客: http://www.only2fire.com/
<p style="text-indent: 2em;">最近在写一个操作文件的函数,使用fseek()函数定位到文件某个位置,然后用fwrite()重新更新该位置的内容,发现该位置处的内容并没有更新,反而更新的内容竟然出现在了文件末尾,真的是不知道什么情况。<br/></p><p style="text-indent: 2em;">下面先简单复现我所遇到的问题吧,示例代码如下:</p><pre class="brush:cpp;toolbar:false PrismJs">/* &nbsp;*By:Ailson&nbsp;Jack &nbsp;*Date:2019.01.24 &nbsp;*Blog:www.only2fire.com &nbsp;*Des:fseek&nbsp;测试 */ #include&nbsp;&lt;stdio.h&gt; int&nbsp;main(void) { &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*fileName&nbsp;=&nbsp;&quot;test.txt&quot;; &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;ch&nbsp;=&nbsp;&#39;W&#39;; &nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*fp&nbsp;=&nbsp;NULL; &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;iRet&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;fp&nbsp;=&nbsp;fopen(fileName,&nbsp;&quot;a+&quot;); &nbsp;&nbsp;&nbsp;&nbsp;if(!fp) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;open&nbsp;%s&nbsp;failed!\r\n&quot;,&nbsp;fileName); &nbsp;&nbsp;&nbsp;&nbsp;iRet&nbsp;=&nbsp;fseek(fp,&nbsp;0,&nbsp;SEEK_SET);&nbsp;/*定位指针到文件头*/ &nbsp;&nbsp;&nbsp;&nbsp;if(iRet&nbsp;&lt;&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;fseek&nbsp;failed!\r\n&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goto&nbsp;exit; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;fwrite(&amp;ch,&nbsp;1,&nbsp;1,&nbsp;fp);&nbsp;/*向文件中写入单个字符&nbsp;&#39;W&#39;*/ exit: &nbsp;&nbsp;&nbsp;&nbsp;fclose(fp); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0; }</pre><p style="text-indent: 2em;">将上述代码编译,然后在编译输出文件所在的文件夹中创建一个test.txt文件,内容为:<span style="color: rgb(0, 112, 192);">123456789abcdef</span>:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2019.01.24/1548329021381124.png" onclick="preview_image(&#39;/uploads/AilsonJack/2019.01.24/1548329021381124.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2019.01.24/1548329021730395.png" onclick="preview_image(&#39;/uploads/AilsonJack/2019.01.24/1548329021730395.png&#39;)"/></p><p style="text-indent: 2em;">接着运行程序,打开test.txt,可以看到内容(字符W)被写到文件的末尾了:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2019.01.24/1548329021633900.png" onclick="preview_image(&#39;/uploads/AilsonJack/2019.01.24/1548329021633900.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2019.01.24/1548329021629642.png" onclick="preview_image(&#39;/uploads/AilsonJack/2019.01.24/1548329021629642.png&#39;)"/></p><p style="text-indent: 2em;">这是什么情况,明明使用fseek定位到文件的开头了,但是实际却写入到了文件末尾,好吧,当时我也是挺困惑的,难道是写fseek()接口函数的哥们还遗留有什么bug。后来上网查证,发现这是我们打开文件的所使用的模式(”a+”)在作怪。下面看看对该模式的描述,直接在终端输入:<span style="color: rgb(255, 0, 0);">man fopen</span></p><p style="text-align:center"><img src="/uploads/AilsonJack/2019.01.24/1548329116157263.png" onclick="preview_image(&#39;/uploads/AilsonJack/2019.01.24/1548329116157263.png&#39;)"/></p><p style="text-indent: 2em;">其中对fopen()函数涉及的a与a+模式的描述如下:<br/><span style="color: rgb(0, 112, 192);">a:Open for appending (<span style="color: rgb(255, 0, 0);">writing at end of file</span>). The file is created if it does not exist. The stream is positioned at the end of the file.<br/>a+:Open for reading and appending (<span style="color: rgb(255, 0, 0);">writing at end of file</span>). The file is created if it does not exist.&nbsp; The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.</span><br/></p><p style="text-indent: 2em;">上述对a与a+模式的描述大致内容是:<span style="color: rgb(255, 0, 0);">打开一个文件,如果该文件不存在将创建文件,初始化的文件读指针位于文件的开头;对于文件的写操作,则始终将写入内容追加到文件的末尾,与文件指针没有关系</span>。<br/></p><p style="text-indent: 2em;">想必看到这里,大家也明白了。如果想使用fseek()函数定位写操作指针,那么就修改fopen()涉及的模式,这里修改为”r+”,就能实现将内容写到文件的开始了,对于自己的程序大家还是根据实际情况修改为相应的模式。<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2019.01.24/1548329021641132.png" onclick="preview_image(&#39;/uploads/AilsonJack/2019.01.24/1548329021641132.png&#39;)"/></p><p style="text-indent: 2em;">如果有什么疑问,欢迎留言交流^_^。</p>
欢迎关注博主的公众号呀,精彩内容随时掌握:
热情邀请仔细浏览下博客中的广告,万一有对自己有用或感兴趣的呢。◕ᴗ◕。。
如果这篇文章对你有帮助,记得点赞和关注博主就行了^_^,当然了能够赞赏博主,那就非常感谢啦!
注: 转载请注明出处,谢谢!^_^
转载请注明来源: 本文链接:  By: AilsonJack
fseek不起作用,写操作定位无效  |  说好一起走
暂无评论,要不要来个沙发
发表评论

 
Copyright © 2015~2023  说好一起走   保留所有权利   |  百度统计  蜀ICP备15004292号