gpt4 book ai didi

c - 文件的移位加密和解密 - 代码无法正常工作

转载 作者:行者123 更新时间:2023-11-30 19:03:28 25 4
gpt4 key购买 nike

当我在代码块中运行它时,我的代码可以正常工作,但是当我尝试打开它的 .exe 文件时,它无法正常工作。请有人帮忙吗?

我是一名编程初学者。这是我正在编写的第一个代码。一个简单的 c 语言凯撒密码。我借助网络的帮助来编写代码。当我在代码块中运行它时,它运行良好,但当我运行 .exe 文件时,它不起作用。输入 key 后它就停止了。我现在不知道该怎么办。

#include <stdio.h>

#define MAXCHAR 1000

int main()
{
int i, x, key;
FILE *fptr;
char str[MAXCHAR], ch, filename[MAXCHAR];

printf("\nPlease enter a File Name: ");
scanf("%s", &filename);
fptr = fopen(filename, "r+");
if (fptr == NULL){
printf("Could not open file %s",filename);
}
while (fgets(str,MAXCHAR, fptr) != NULL);
printf("\nPlease choose following options:\n");
printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);
printf("Enter key: ");
scanf("%d", &key);
switch(x)
{
case 1:
for(i = 0; str[i] != '\0'; ++i){
ch = str[i];

if(ch >= 'a' && ch <= 'z'){
ch = ch + key;

if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}

str[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;

if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}

str[i] = ch;
}
}
printf("Encrypted string: %s\n", str);
break;

case 2:

for(i = 0; str[i] != '\0'; ++i){
ch = str[i];

if(ch >= 'a' && ch <= 'z'){
ch = ch - key;

if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}

str[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;

if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}

str[i] = ch;
}
}
printf("Decrypted string: %s\n", str);
break;

default:
printf("\nError\n");
}

return 0;
}

这是我试图满足的要求。

Implementation:

  1. File encryption
  2. File decryption

Note:

Read the contents(plain text) from a file (only English letter allowed) as characters, in terms of the KEY (input from keyboard by user ) shift the corresponding character(get cipher text), and do the reverse operation when decrypting the file.

For example:

Assume the plain text is “abcdef”, KEY is 5, then every single letter in the plain text will shift right by 5 positions according to the alphabet( the following letter after z is a), get the cipher text (garbled)“fghijkl”.

Now decrypt the cipher text: fghijkl, KEY is 5, and then every single letter in the cipher text will be shift left by 5 positions according to the alphabet( the following letter after a is z), get the original text “abcdef”.

最佳答案

您提到的代码几乎没有问题。首先这个

scanf("%s", &filename); /* & is not needed */

这是错误的,编译器可能会警告你

error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[1000]’ [-Werror=format=]

如果您使用 -Wall -Wstrict-prototypes -Werror 等标志编译代码。删除 & 因为 filename 是 char 数组和数组名称本身的地址,正确的是

scanf("%s", filename);

其次,这里

while (fgets(str,MAXCHAR, fptr) != NULL); /* dummy loop */
while 末尾的

分号 ; 是故意的还是拼写错误?由于 ; 当循环失败时 str 包含 NULL。你可能想要像

while (fgets(str,MAXCHAR, fptr) != NULL) { /* some code */ }

此外,fgets() 将新行存储在缓冲区末尾,您可能需要删除尾随的 \n。例如

while(fgets(str, MAXCHAR, fptr) != NULL) {
str[strcspn(str, "\n")] = 0; /* remove the trailing \n */
/* some code */
}

最后来了

if (fptr == NULL){
printf("Could not open file %s",filename);
}

fopen() 处理不正确,就好像文件不存在或 fopen() 失败一样,它只是打印消息并进一步继续并使用 fptr 不应该使用,请使用 return 0exit(0)。例如

if (fptr == NULL){
fprintf(stderr,"Could not open file %s",filename);
return 0; /* this is must */
}

示例代码:

#define MAXCHAR 1000
int main(void) {
int i, x, key;
FILE *fptr;
char str[MAXCHAR], ch, filename[MAXCHAR]/* I don't think filename will be that much big, use accordingly */;
printf("\nPlease enter a File Name: ");
scanf("%s", &filename); /* & is not required as filename itself address */
fptr = fopen(filename, "r+");
if (fptr == NULL){
printf("Could not open file %s",filename);
return 0; /* add this else it proceeed further */
}
while (fgets(str,MAXCHAR, fptr) != NULL) { /* remove semicolon, read each line from file, store into str and do the operation with str */
printf("\nPlease choose following options:\n");
printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);
printf("Enter key: ");
scanf("%d", &key);
switch(x) {
case 1:
for(i = 0; str[i] != '\0'; ++i){
ch = str[i];

if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}

str[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}

str[i] = ch;
}
}
printf("Encrypted string: %s\n", str);
break;
case 2:
for(i = 0; str[i] != '\0'; ++i){
ch = str[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}

str[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;

if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}

str[i] = ch;
}
}
printf("Decrypted string: %s\n", str);
break;

default:
printf("\nError\n");

}
}
return 0;
}

关于c - 文件的移位加密和解密 - 代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53975926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com