[英]C crypto program with stdin, stdout, stderr and error message
我正在研究应该是一个简单的c程序,该程序需要两个args字符e或d和一个键。 如果是e,它将加密,如果是d,则在两种情况下都使用密钥解密。 如果有错误,它将从stdin读取并输出到stdout或stderr。 我收到警告消息
* cypher.c:30:4:警告:传递'fputc'的参数1会使指针不带强制转换的整数[默认启用] /usr/include/stdio.h:579:12:注意:预期为'int',但参数的类型为'char
该程序进行编译和编码,但如果将d或e以外的其他字符按原样传递给char,则解码似乎也不起作用,也不会引发错误。 任何帮助将不胜感激。
*已进行了编辑,以解决一些问题,例如last fputc()现在为fputs(),将i ++重新添加到last循环中,并将if(ende = e)替换为if(ende ==“ e” )。 错误代码不再是问题,但程序功能似乎仍然存在。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main(char ende, char key[150]){
int e;
int i=0;
int c=fgetc(stdin);
int n=strlen(key);
if(ende == "e"){
while(c != EOF){
c= fgetc(stdin);
e=(c - 32 + key[i % n]) % 95 + 32;
fputc( e, stdout);
i++;
}
}
else if (ende == "d"){
while(e != EOF){
e= fgetc(stdin);
c=(e - 32 - key[i % n] + 3 *95) %95 + 32;
fputc( c, stdout);
i++
}
}
else{
fputs("you broke it",stderr);
exit (1);
}
exit (0);
}
if (ende = e)
出了点问题,也许是if (ende == e)
和else if (ende == d)
fputc("you broke it",stderr);
fputc()
将int
作为第一个参数,它应该是:
fprintf(stderr, "you broke it");
而且您的main()
不是标准的:
main(char ende, char key[150])
标准main
应该是int main(int argc, char* argv[]
,您可以使用argc
和argv
以外的其他名称,但是类型仍然不匹配。
尝试这个:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
main(int argc, char *argv[]) {
int e, c, n, i;
char *key, *ende;
i = 0;
ende = argv[1];
key = argv[2];
n = strlen(key);
c = fgetc(stdin);
if (strcmp(ende, "e") == 0) {
while(c != EOF){
e=(c - 32 + key[i % n]) % 95 + 32;
fputc( e, stdout);
i++;
c= fgetc(stdin);
}
}
else if (strcmp(ende, "d") == 0) {
while(c != EOF){
e=(c - 32 - key[i % n] + 3 *95) %95 + 32;
fputc( e, stdout);
i++;
c= fgetc(stdin);
}
}
else{
fputs("you broke it",stderr);
exit (1);
}
exit (0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.