C-我需要重命名该文件,但是我不明白错误的内容,文件已加密,但名称不会更改



我需要重命名文件,但是我不明白错了,文件已加密,但是名称不更改,如何正确使用Rename((函数。我需要将文件名更改为" Encrypt.yes"

#include <stdio.h>                                                                                  
#include <stdlib.h>  
#include <string.h>

int main(void)                                                                                       
{   
char new[20];                                                                                                
char old[20];
int rename(const char *old, const char *new);                                                                                 
int ch;                                                                                         
FILE *fps;                                                                            
printf("Enter file name (with extension like file.txt) to encrypt : ");
strcpy(new,"encrypt.yes");
rename(old, new);                                
scanf("%s", old);
fps = fopen(old, "r+");                                           
if (fps == NULL) {                                                                              
    printf("Could not open file '%s'n", old);                                                
    return 1;                                                                                   
}                                                          
while ((ch = fgetc(fps)) != EOF) {                                                              
    ch += 100;                                                                                  
    fseek(fps, -1, SEEK_CUR);                                                                   
    fputc(ch, fps);                                                                             
    fseek(fps, 0, SEEK_CUR);
}                                                 
fclose(fps);                                                                                    
printf("File '%s' encrypted successfullyn", old);                                            
return 0;                                                                                       

}

不知道我是否完全了解您,但我认为您正在尝试修改每个字节。将此答案适应您的代码,您可以做以下类似的事情。注意:仍然有很多改进和错误处理的余地。

另外,我知道您将其标记为Windows,但是我的Windows VM目前正在搞砸,因此我使用GCC在Linux上编写并测试了此代码。它可能在Windows AS上工作,但我没有测试它。尽管如此,我认为这足够直接,可以让您95%。钥匙是以" R "模式打开文件,因此您可以从中读取并写入。

最后,不要掩饰您与Paul Ogilvie和David C. Rankin这么远的评论;这是很好的建议。

#include <stdio.h>                                                                                  
#include <stdlib.h>                                                                                 
int main()                                                                                          
{                                                                                                   
    char fname[20];                                                                                 
    int ch;                                                                                         
    FILE *fps;                                                                                      
    printf("Enter file name (with extension like file.txt) to encrypt : ");                         
    scanf("%s", fname);                                                                             
    fps = fopen(fname, "r+");                                                                       
    if (fps == NULL) {                                                                              
        printf("Could not open file '%s'n", fname);                                                
        return 1;                                                                                   
    }                                                                                               
    while ((ch = fgetc(fps)) != EOF) {                                                              
        ch += 100;                                                                                  
        fseek(fps, -1, SEEK_CUR);                                                                   
        fputc(ch, fps);                                                                             
        fseek(fps, 0, SEEK_CUR);                                                                    
    }                                                                                               
    fclose(fps);                                                                                    
    printf("File '%s' encrypted successfullyn", fname);                                            
    return 0;                                                                                       
}  

最新更新