试下这个:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Dmalloc
malloc
#define Drealloc realloc
#define Dfree free
#define FS_Open(name, oflag)
fopen(name, oflag)
#define FS_Close(fp)
fclose(fp)
#define FS_Seek(fp, offset, whence)
fseek(fp, offset, whence)
#define FS_Tell(fp)
ftell(fp)
#define FS_Read(ptr, size, nitem, fp)
fread(ptr, size, nitem, fp)
#define FS_Write(ptr, size, nitem, fp)
fwrite(ptr, size, nitem, fp)
void main(void)
{
FILE *stream,*stream2;
char *sfilepath="d:\\mcutest\\tc\\fu\\file3.txt";
char *dfilepath="d:\\mcutest\\tc\\fu\\file4.txt";
char *chrTarget,*chrSource;
char *chrbegin;
int inifilesize,intnumread,intpos,intsum,intTemp1,intTemp2;
if( (stream = FS_Open( sfilepath, "r+b" )) == NULL )
{
printf( "The file 'test.txt' was not opened or not exist!\n" );
}
else
{
if (FS_Seek(stream, 0, SEEK_END)!= 0) //move the file point the end of file
{
printf( "move the file point to the end of file failed!\n" );
}
inifilesize=FS_Tell(stream);//get current file size
if (FS_Seek(stream, 0, SEEK_SET)!= 0) //move the file point to the begin of file again
{
printf( "move the file point to the begin of file failed!\n" );
}
chrSource=(char*)Dmalloc(inifilesize);
if( NULL==chrSource )
{
printf( "Insufficient memory available\n" );
}
intnumread = FS_Read( chrSource, sizeof( char ), inifilesize, stream );//get file stream
chrbegin=chrSource;
chrTarget=(char*)Dmalloc(inifilesize);
if( NULL==chrTarget )
{
printf( "Insufficient memory available\n" );
}
printf("******************************************************************************\n");
printf("The contents of test.txt is showed as below:\n%s\n", chrSource );
intsum=0;
for (intTemp1=0;intTemp1<inifilesize;intTemp1++)
{
intpos=strcspn(chrbegin,"mA");
for (intTemp2=0;intTemp2<intpos;intTemp2++)
{
if (chrbegin-chrSource>=inifilesize)
break;
*(chrTarget+intsum)=*chrbegin;
chrbegin++;
intsum++;
}
if (chrbegin-chrSource>=inifilesize)
break;
else
chrbegin+=strlen("mA");
}
*(chrTarget+intsum)=0;
printf("%s ",chrTarget);
if( (stream2 = FS_Open( dfilepath, "a" )) == NULL )
{
printf( "The file 'test.txt' was not opened or not exist!\n" );
}
intnumread = FS_Write( chrTarget, sizeof( char ), strlen(chrTarget), stream2 );
printf("the number of characters had been writeen is:%d",intnumread);
}
Dfree(chrSource);
Dfree(chrTarget);
FS_Close(stream);
FS_Close(stream2);
}