调用C语言库函数,在文本中添加和删除这段(红色标记)
End Extended Reserved Data:
[Reserved Data]
0x4f505448
0x00000002
0x45524158
0x0000000c
0x00000000
0x5465726D
0x00000008
End Reserved Data
[Image List]
测试代码
#include <stdio.h>
#include <string.h>
void
main()
{
int
nCount,erase=0;
//erase=0 删除这段,erase=1添加这段
if
(erase==0)
{
FILE
*pFile=
fopen
(
"test.txt"
,
"r+"
);
if
(pFile==NULL)
{}
fseek
(pFile,0,SEEK_END);
nCount =
ftell
(pFile);
//算出文件长度
fseek
(pFile,0,SEEK_SET);
char
*pBuff =
new
char
[nCount+1];
char
*pBuffTemp =
new
char
[nCount+1];
memset
(pBuff,0,nCount+1);
memset
(pBuffTemp,0,nCount+1);
char
*pBuff1,*pBuff2;
fread
(pBuff,1,nCount,pFile);
fclose
(pFile);
pBuff1=
strstr
(pBuff,
"End Extended Reserved Data:"
)
//指针定位
pBuff2=
strstr
(pBuff,
"[Image List"
);
//指针定位
strcpy
(pBuffTemp,pBuff2);
pBuff1 = pBuff1+28;
pBuff1[0]=0;
strcat
(pBuff,pBuffTemp);
nCount=
strlen
(pBuff)+1;
FILE
*pFile1 =
fopen
(
"test.txt"
,
"w+"
);
//重新打开该文件,清空内容
fwrite
(pBuff,1,nCount,pFile1);
//写入缓存中的数据
fclose
(pFile1);
}
else
if
(erase == 1)
{
char
StringBuff[]=
"[Reserved Data]\n0x4f505448\n0x00000002\n0x45524153\n0x0000000c\n0x00000000\n0x5465726D\n0x00000008\nEnd Reserved Data\n"
;
FILE
*pFile=
fopen
(
"test.txt"
,
"r+"
);
if
(pFile==NULL)
{}
fseek
(pFile,0,SEEK_END);
nCount=
ftell
(pFile);
fseek
(pFile,0,SEEK_SET);
char
*pBuff=
new
char
[nCount+1];
char
*pBuffTemp =
new
char
[nCount+1];
memset
(pBuff,0,nCount+1);
memset
(pBuffTemp,0,nCount+1);
char
*pBuff1,*pBuff2;
fread
(pBuff,1,nCount,pFile);
fclose
(pFile);
pBuff1=
strstr
(pBuff,
"End Extended Reserved Data:"
);
pBuff1=pBuff1+28;
pBuff2=
strstr
(pBuff,
"[Image List]"
);
strcpy
(pBuffTemp,pBuff2);
strcpy
(pBuff1,StringBuff);
pBuff1=pBuff1+
strlen
(StringBuff)+1;
pBuff[0]=0;
strcat
(pBuff,pBuffTemp);
nCount=
strlen
(pBuff)+1;
FILE
*pFile1=
fopen
(
"test.txt"
,
"w+"
);
fwrite
(pBuff,1,nCount,pFile1);
fclose
(pFile1);
}
}