【问题标题】:Argc not working for my C ProgArgc 不适用于我的 C 程序
【发布时间】:2018-01-04 11:30:39
【问题描述】:

I get Core Dumped when I'm having 3 arg. Please help!

我正在编写一个程序来加密单词。问题是,该程序运行良好,但是当我输入 2/3 arg 而不是 4 时,我会转储核心。请帮我。 下面,我已经包含了代码。上面(第一次使用这个平台,如果它真的在上面,那么idk)是错误的图片。感谢大家的帮助!

这是我的代码:

#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

        }

【问题讨论】:

  • 我梦想有一天人们不会从 C 开始学习编程。提示:C 中的索引从 0 开始。

标签: c core argc


【解决方案1】:

在检查它们是否存在之前,您使用了 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 的元素存在之前不要访问它们。

【讨论】:

  • 只有在需要时才会声明 Min 和 Max。
  • @Stargateur:我遵循了 OP 的现有风格,即在块的顶部声明所有(嗯,大多数)变量,而不是第一次使用点。 C99 和更高版本都可以,但我想我会避免进一步弄乱他们现有的代码组织;有些人真的很喜欢将他们的声明分组。 :-)
  • @ShadowRanger:谢谢你的回答,真的很感激:D @Stargateur:对不起,伙计。我只是一个学生。不要因为我是这方面的初学者而对我太苛刻。甚至我希望改进。但是我会注意你们说的!再次感谢:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多