Argc不适用于我的C程序
我正在写一个加密单词的程序。问题是,程序运行得很好,但当我输入2/3 arg而不是4 arg时,我会将核心转储。请帮帮我。 下面,我已经包含了代码。上面(第一次使用这个平台,所以idk如果它真的在上面)是错误的图片。谢谢你的帮助 这是我的代码:Argc不适用于我的C程序,c,core,argc,C,Core,Argc,我正在写一个加密单词的程序。问题是,程序运行得很好,但当我输入2/3 arg而不是4 arg时,我会将核心转储。请帮帮我。 下面,我已经包含了代码。上面(第一次使用这个平台,所以idk如果它真的在上面)是错误的图片。谢谢你的帮助 这是我的代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <crypt.h> #include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crypt.h>
#include <time.h>
int DisplayHelp(){
printf("Usage :./task1 <wordlist> <min> <max>\n");
printf("\t<wordlist> : A file path/name in which contains the password dictionary \n");
printf("\t<min> : An integer value greater than 1.\n");
printf("\t\tThis value represents the minimum length of the password\n");
printf("\t<max> : An integer value greater than or equal to <min>.\n");
printf("\t\t<max> represents the maximum length of the password.\n");
exit(1);
}
int main(int argc, char * argv[] ){
char * type1="$1$";
char * type6="$6$";
char * salt="$";
char * mdresult;
char encyption_scheme[20];
FILE * fp;
FILE * fp1;
char input1 [50];
int length;
int line=0;
int line2=0;
int Min=atoi(argv[2]);
int Max=atoi(argv[3]);
if(Min>Max || argc!=4){
DisplayHelp();
}
fp = fopen(argv[1],"r"); //Reading of file
if(fp == NULL){
printf("Fatal error! %s file not found\n",argv[1]);
printf("Program halted. Please verify the file path and try again.\n");
exit(1);
}
fclose(fp);
time_t current_time = time(NULL);
struct tm *tm = localtime(¤t_time);
printf("Program Started At %s\n", asctime(tm));
fp=fopen(argv[1],"r");
while(fgets(input1,50,fp)){
//line++; //Line +1
length=strlen(input1)-1;
input1[strlen(input1)-1]='\0';
if(length >= Min && length <= Max){
line++;
strcpy(encyption_scheme,type1);
strcat(encyption_scheme,salt);
mdresult = crypt(input1,encyption_scheme);
line2++;
strcpy(encyption_scheme,type6);
strcat(encyption_scheme,salt);
sharesult = crypt(input1,encyption_scheme);
line2++;
fp1=fopen("Filename.txt","a");
if (fp1==NULL)
exit(-1);
fprintf(fp1, "%s:%s\n", input1, mdresult);
fprintf(fp1, "%s:%s\n", input1, sharesult);
fclose(fp1);
}
}
printf("Total number of words processed => %d\n", line);
printf("Total number of generated entries => %d\n", line2);
fclose(fp);
current_time = time(NULL);
tm = localtime(¤t_time);
printf("\n");
printf("Program Ended At %s\n", asctime(tm)); //Add time
}
#包括
#包括
#包括
#包括
#包括
int DisplayHelp(){
printf(“用法:./task1\n”);
printf(“\t:包含密码字典的文件路径/名称\n”);
printf(“\t:大于1的整数值。\n”);
printf(“\t\t此值表示密码的最小长度\n”);
printf(“\t:大于或等于的整数值。\n”);
printf(“\t\t表示密码的最大长度。\n”);
出口(1);
}
intmain(intargc,char*argv[]){
char*type1=“$1$”;
char*type6=“$6$”;
char*salt=“$”;
char*mdresult;
字符加密方案[20];
文件*fp;
文件*fp1;
字符输入1[50];
整数长度;
内线=0;
int line2=0;
int Min=atoi(argv[2]);
int Max=atoi(argv[3]);
如果(最小值>最大值| | argc!=4){
显示帮助();
}
fp=fopen(argv[1],“r”);//读取文件
如果(fp==NULL){
printf(“致命错误!%s文件找不到\n”,argv[1]);
printf(“程序已停止。请验证文件路径并重试。\n”);
出口(1);
}
fclose(fp);
时间=当前时间=时间(空);
struct tm*tm=localtime(¤t_time);
printf(“程序开始于%s\n”,asctime(tm));
fp=fopen(argv[1],“r”);
而(FGET(输入1,50,fp)){
//line++;//line+1
长度=strlen(输入1)-1;
input1[strlen(input1)-1]='\0';
如果(length>=Min&&length您在检查它们是否存在之前使用了argv[2]
和argv[3]
。更改:
int Min=atoi(argv[2]);
int Max=atoi(argv[3]);
if(Min>Max || argc!=4){
DisplayHelp();
}
例如:
int Min, Max;
if (argc != 4) DisplayHelp();
Min = atoi(argv[2]);
Max = atoi(argv[3]);
if (Min > Max) DisplayHelp();
因此,在验证argv
元素是否存在之前,您不能访问这些元素。我梦想有一天人们不会从C开始学习编程。提示:C中的索引从0开始。只有在需要时才会声明最小值和最大值。@Stargateur:我遵循OP现有的声明所有(嗯,大多数)的风格位于块顶部的变量,而不是第一个使用点。C99和更高版本两者都可以,但我想我应该避免进一步弄乱它们现有的代码组织;有些人真的很喜欢将它们的声明分组。:-)@暗影游侠:谢谢你的回答,真的很感激:D@Stargateur:对不起,伙计。我只是个学生。不要因为我是初学者就对我太苛刻。即使我希望提高。但我会记下你们说的话!再次感谢:D