Argc对我的C程序不起作用,正在进行核心转储

qoefvg9y  于 2023-03-01  发布在  其他
关注(0)|答案(1)|浏览(100)

我正在写一个加密单词的程序,问题是,程序运行正常,但是当我输入2/3而不是4个arg时,我会得到核心转储:

这是我的代码:

#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(&current_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(&current_time);
            printf("\n");
            printf("Program Ended At %s\n", asctime(tm)); //Add time

        }
2wnc66cl

2wnc66cl1#

您在检查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的元素存在之前,您不会访问它们。

相关问题