稍微改造了一下
#include <stdio.h>
#include <string.h>
#include <io.h>
void findOutput(char *fileName, char *sign);
void main( void )
{
// 这里文件路径和所要查找的标记可以指定用户输入
// ......
// ......
findOutput("C:\\FileName.txt","TOOL:");
}
void findOutput(char *fileName, char *sign) {
unsigned int fhand;
unsigned int fileLen;
unsigned int bytesRead;
char *pDestStart;
char *pDestEnd;
char *buffer;
char result[10000];
// Open file (FileName.txt)
if((fhand = _open( fileName, 0x0000)) == -1)
printf( "File not opened\n" );
else {
fileLen = _filelength( fhand ) + 1;
buffer = new char[fileLen];
if(( bytesRead = _read( fhand, buffer, fileLen )) <= 0)
printf( "File access error!\n" );
else {
pDestStart = buffer;
while(1) {
pDestStart = strstr(pDestStart,sign);
if (pDestStart == NULL)
break;
pDestStart += 6;
pDestEnd = pDestStart;
pDestEnd = strstr(pDestEnd,"\n");
if (pDestEnd == NULL) {
strncpy(result, pDestStart, fileLen - strlen(pDestStart));
result[fileLen - strlen(pDestStart)] = 0;
printf("%s\n",result);
break;
}
strncpy(result, pDestStart, pDestEnd - pDestStart);
result[pDestEnd - pDestStart] = 0;
pDestEnd += 1;
pDestStart = pDestEnd;
printf("%s\n",result);
}
}
}
_close(fhand);
}