我一直在练习C,所以我认为制作一个文件加密和解密程序是一个很好的练习。然而,当我在处理在终端上显示文件内容的加密形式的问题时,我遇到了一个挑战,即在终端上展示我存储在缓冲区中的字符。当我使用printf函数时显示的值似乎不完整,当我使用for循环迭代并打印缓冲区中每个字符时,一些字符最终被忽略了。我试过调试,但几乎没有任何进展。如果有人能告诉我出了什么问题,我将不胜感激。
  
  
#include <stdio.h>
#include <stdbool.h>
void encrypt(char* path );
void decrypt(char* );
int main(void){
    char input;
    printf("Hello, please enter the path of the file you want to encrypt\n");
    char path[1024];
    scanf("%s",path);
    printf("File Path:%s\nFile encrypted content: ",path);
    FILE *file = fopen(path, "r+");
    if(file==NULL){
        printf("Cannot open file \n");
        
        system("pause");
        return 1;
    }
    int ch;
    int count = 0;
    char buffer[1024];
    
    while((ch=fgetc(file))!= EOF){
        int hc = (ch + (count*2))%128;
        printf("Original character: %c (ASCII value: %d) ", ch, ch);
        
        buffer[count] = hc;
        count++;
        printf("Encrypted character: %c (ASCII value: %d)\n ", hc, hc);
        if (count >= sizeof(buffer)-1){
            printf("Buffer size exceeded");
            break;
        }
        
        
    }
    
    buffer[count] = '\0';
    printf("This is the complete buffer\n\n");
    for(int i=0; i<= count; i++){
        
        printf("%c", buffer[i]);
    }
    printf("\nThis is the buffer printed as a string %s", buffer);
    fclose(file);
    
   system("pause");
   return 0;
}
  
   我最初用一个大约三个单词的简单文本文件作为输入运行程序,但当加密字符打印出来时,它们比预期的要少。在加密循环运行时,我尝试用加密形式打印每个字符,结果还可以。缓冲区中存储的所有值都已成功加密。但是当我试图使用printf在缓冲区中打印我们的值时,问题仍然存在。我尝试使用for循环,它打印了我们几乎所有的字符,有些字符被遗漏了。