RT,指定字符串一定是跨行的
我的代码是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
#define SUCCESS 0
int main(){
FILE *fp;
char cBuf[N+1];
char cRes[40960] = {0};
}
按行读取后用strcat将各行连接到一个字符串中就行了。或者用fread读取
修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
#define SUCCESS 0
int test(char *pcBuf, char *pcRes);
int main(){
FILE *fp;
char cBuf[N+1];
char cRes[40960] = {0};
long lenght = 0;
if( (fp=fopen("d:\\demo.txt","rt")) == NULL ){
puts("Fail to open file!");
//exit(0);
}
else{
fseek(fp,0L,SEEK_END);
lenght = ftell(fp);
rewind(fp);
fread(cBuf,1,lenght,fp);
fclose(fp);
printf("%s\n", cBuf);
test(cBuf, cRes);
printf("%s\n", cRes);
}
//while(fgets(cBuf, N, fp) != NULL){
//printf("%s", cBuf);
//test(cBuf, cRes);
//printf("%s\n", cRes);
//}
//fclose(fp);
return 0;
}
int test(char *pcBuf, char *pcRes)
{
char *pcBegin = NULL;
char *pcEnd = NULL;
pcBegin = strstr(pcBuf, "hello");
pcEnd = strstr(pcBuf, "yu");
if(pcBegin == NULL || pcEnd == NULL || pcBegin > pcEnd)
{
printf("not found!\n");
}
else{
pcBegin += strlen("hello ");//修改
memcpy(pcRes, pcBegin, pcEnd-pcBegin);
}
return SUCCESS;
}