当我在代码块中运行它时,我的代码正在工作,但是当我尝试打开它的.exe文件时,它在那儿无法正常工作。请问有人帮忙吗?
我是编程的初学者。这是我正在处理的第一个代码。 c中的一个简单的凯撒密码。我从网上寻求帮助来编写代码。当我在代码块中运行它时,它运行良好,但是当我运行.exe文件时,它无法正常工作。输入键后停止。我不知道现在该怎么办。
#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;
}
这是我试图满足的要求。
实施:
- 文件加密
- 文件解密
注意:
从文件(仅允许英文字母)中读取内容(纯文本)作为字符,根据KEY(用户从键盘输入)移动相应的字符(获取密文),并在出现以下情况时执行相反的操作解密文件。
例如:
假设纯文本为“ abcdef”,KEY为5,则纯文本中的每个字母将根据字母表向右移动5个位置(z之后的字母为a),得到密文(乱码) )“ fghijkl”。
现在解密密文:fghijkl,KEY为5,然后密文中的每个字母将根据字母向左移5个位置(a之后的字母为z),得到原始文本“ abcdef”。
答案 0 :(得分:1)
您提到的代码几乎没有问题。首先是这个
scanf("%s", &filename); /* & is not needed */
是错误的,编译器可能会警告您
错误:格式为'%s'的参数类型为'char *',但参数2 的类型为'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 0
或exit(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;
}