【发布时间】:2020-07-13 17:39:21
【问题描述】:
是否可以存储缓冲区的最后 n-1 个字符,然后将其附加到新缓冲区的开头?例如,如果我从文件中读取数据并将其存储在大小为 1000 的缓冲区中,是否可以仅保留当前缓冲区的最后 n-1 个字符并将其带到新缓冲区的开头阅读接下来的 1000 个字符。 我不想从文件中重新读取数据。只需从旧缓冲区中保存几个字符并将其放入新缓冲区的开头即可。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fptr;
int l,count=0,index;
char name[100],word[25],buffer[1000],*pos;
printf("\nEnter the word to be found:");
scanf("%s",word);
l=strlen(word);
printf("\nEnter the file name:");
scanf("%s",name);
fptr=fopen(name,"r");
if(fptr==NULL){
printf("\nProblem with opening the file");
exit(1);
}
while ((fgets(buffer, 1000, fptr)) != NULL)
{
index = 0;
while ((pos = strstr(buffer + index, word)) != NULL)
{
index = (pos - buffer) + 1;
count++;
}
}
printf("The word %s is found %d times",word,count);
fclose(fptr);
}
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。