fseek不起作用,写操作定位无效
<p style="text-indent: 2em;">最近在写一个操作文件的函数,使用fseek()函数定位到文件某个位置,然后用fwrite()重新更新该位置的内容,发现该位置处的内容并没有更新,反而更新的内容竟然出现在了文件末尾,真的是不知道什么情况。<br/></p><p style="text-indent: 2em;">下面先简单复现我所遇到的问题吧,示例代码如下:</p><pre class="brush:cpp;toolbar:false PrismJs">/*
*By:Ailson Jack
*Date:2019.01.24
*Blog:www.only2fire.com
*Des:fseek 测试
*/
#include <stdio.h>
int main(void)
{
char *fileName = "test.txt";
char ch = 'W';
FILE *fp = NULL;
int iRet = 0;
fp = fopen(fileName, "a+");
if(!fp)
printf("open %s failed!\r\n", fileName);
iRet = fseek(fp, 0, SEEK_SET); /*定位指针到文件头*/
if(iRet < 0)
{
printf("fseek failed!\r\n");
goto exit;
}
fwrite(&ch, 1, 1, fp); /*向文件中写入单个字符 'W'*/
exit:
fclose(fp);
return 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('/uploads/AilsonJack/2019.01.24/1548329021381124.png')"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2019.01.24/1548329021730395.png" onclick="preview_image('/uploads/AilsonJack/2019.01.24/1548329021730395.png')"/></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('/uploads/AilsonJack/2019.01.24/1548329021633900.png')"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2019.01.24/1548329021629642.png" onclick="preview_image('/uploads/AilsonJack/2019.01.24/1548329021629642.png')"/></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('/uploads/AilsonJack/2019.01.24/1548329116157263.png')"/></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. 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('/uploads/AilsonJack/2019.01.24/1548329021641132.png')"/></p><p style="text-indent: 2em;">如果有什么疑问,欢迎留言交流^_^。</p>
暂无评论,要不要来个沙发
发表评论
JLink V9掉固件修复(灯不亮) 3Zephyr笔记2 - 在STM32F429上运行HelloWorld 2计算NandFlash要传入的行地址和列地址 1Linux MMC子系统 - 6.eMMC 5.1工作模式-设备识别模式 0Linux MMC子系统 - 5.eMMC 5.1工作模式-引导模式 0Linux MMC子系统 - 4.eMMC 5.1常用命令说明(2) 0
标签云
Linux嵌入式实用技巧ARM内核学习问题集合CC++编程语言阅读笔记汇编Linux内核完全注释Windows驱动开发计算机基础ARM11ARMv7-ASTM32IDESublimeLinux内核学习eMMCMMC子系统Ubuntu操作系统OfficeVMWareAPUEgccRTOS中断漫游世界随笔感悟开发工具软件应用编程VsCodearmccarmclang编译器ZephyrSPIJLink网卡驱动安装各种芯片库函数NFSμCOS内核sambaFlashUnix命令与脚本输入法Linux内核设计与实现gitRIFFWAVJATGFTPar8161安装centos有线上网μCGUI字库工程建立右键菜单网络文件系统Firefox百度NTFS文件系统CodeBlocksCentOS数据结构算法PhotoShop51KeilQTUltraEditscanfglibc宏定义UIDGID优先级娱乐天地SourceInsight磁盘扇区总线I2CPDFBComparePythonI2SFPUMakefileSWDCPUARP软件推荐FileZilla