今天下午的时候调试如下一段代码,其他均正常,但文件写操作始终不能成功。单步执行踪文件写的过程,发现返回值是正确的,非常奇怪!该段代码的大致作用是,以"r+"的方式打开一个已经存在的文件,并读取文件中的内容,当读取到特定内容时,将内容修改再回写到原文件的存储位置。简单来讲就是对文件的,“读-修改-写”过程。
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *pfile = NULL;
unsigned char i = 0;
char temp[1000],data[10];
long num;
char *pstr;
char *tempstr;
int cnt;
pfile = fopen("product_info.h","r+");
if(pfile != NULL)
{
while((i < 100)&&(fgets(temp,1000,pfile) != NULL))//读取一行字符
{
pstr = strstr(temp,"pd_sft_ver");//查找软件版本标记
if(pstr)//找到软件版本标记行
{
pstr = strstr(temp,"{");//查找起始括弧
if(pstr)//找到起始括弧
{
pstr += 10;
num = strtol(pstr,&tempstr,10);
num++;