C 是否可以在缓冲区的末尾仅存储n-1个字符?

C 是否可以在缓冲区的末尾仅存储n-1个字符?,c,file-io,buffer,C,File Io,Buffer,是否可以存储缓冲区的最后n-1个字符,然后将其附加到新缓冲区的开头?例如,如果我从文件中读取数据并将其存储在大小为1000的缓冲区中,是否可以只保留当前缓冲区的最后n-1个字符,并将其带到新缓冲区的开始处,新缓冲区将读取下一个1000个字符我不想从文件中重新读取数据。只需从旧缓冲区中保存几个字符,并将其放入新缓冲区的开头 #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <

是否可以存储缓冲区的最后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);
}
#包括
#包括
#包括
#包括
int main(){
文件*fptr;
int l,计数=0,索引;
字符名[100],字[25],缓冲区[1000],*pos;
printf(“\n输入要查找的单词:”);
scanf(“%s”,单词);
l=strlen(单词);
printf(“\n输入文件名:”);
scanf(“%s”,名称);
fptr=fopen(名称,“r”);
如果(fptr==NULL){
printf(“\n打开文件的问题”);
出口(1);
}
while((fgets(缓冲区,1000,fptr))!=NULL)
{
指数=0;
while((pos=strstrstr(缓冲区+索引,字))!=NULL)
{
索引=(位置缓冲)+1;
计数++;
}
}
printf(“单词%s被找到%d次”,单词,计数);
fclose(fptr);
}

您是正确的-如果您正在计算某个特定单词的出现次数并以较小的部分读取文件,则需要注意并处理只有单词的初始部分位于缓冲区末尾的情况

如果当前缓冲区包含一个单词的一部分,则可以将该“半”字复制到缓冲区的开头,然后在下一个
fread
中给出另一个指针和另一个长度

比如:

#define BUF_SZ 1000

char buffer[BUF_SZ];

// Full file read
fread(buffer, 1, BUF_SZ, fp);

// do some stuff
. . .

numbers_in_half_word = 3; // Just as example. In the real code you need
                          // to calculate it based on the first input

// Copy to start of buffer
memcpy(buffer, buffer + (BUF_SZ - numbers_in_half_word), numbers_in_half_word);

// Reduced file read, i.e. max 997 chars - note: ptr moved by 3, length reduced by 3
fread(buffer + numbers_in_half_word, 1, BUF_SZ - numbers_in_half_word, fp);
编辑

OP刚刚发布了代码,显示使用了
fgets
。原则仍然是一样的:

int numbers_in_half_word = 0;  // No half-word the first time. So init to zero

while ((fgets(buffer + numbers_in_half_word, 1000 - numbers_in_half_word, fptr)) != NULL)
{

   // do stuff including calculation of the new numbers_in_half_word value

   if (numbers_in_half_word)
   {
       memcpy(buffer, buffer + (1000 - 1 - numbers_in_half_word), numbers_in_half_word);
   }
}
请注意memcpy中的-1。这是必需的,因为
fgets
使用
buffer[999]
作为零终止

特殊注意事项

如果半个单词(又称“半个单词中的数字”)等于或大于500,则
memmove
优于
memcpy

如果半个单词(又称“半个单词中的数字”)为999,则上面的代码将进入无休止的循环,因为文件中没有新字符的空间


一个好的程序应该能够处理这样的情况,即使它们不太可能出现在一个单词文件中。

通过半个单词中的数字,你的意思是要搜索的单词中与字符串匹配的字符数,对吗?@SharonShelton让我们假设你正在寻找“世界”,而你的缓冲区是“很多单词……你好,世界”您需要将最后3个字母(即“wor”)复制到缓冲区起始位置,然后执行下一个
fgets
注释不用于扩展讨论;这段对话已经结束。