运行时获取分段核心转储错误

时间:2017-02-09 02:25:30

标签: c segmentation-fault

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void encrypt(char inputText[20], int inputLength, int key);
void decrypt(int cipherText[20], int inputLength, int key);


FILE* fp;
char* mappingFile;

int main(int argc, char* argv[]){

  char inputText[20],temp;
  int key,mode,inputLength, cipherText[20],i;
  if (strcmp(argv[1], "-i")==0){
      mappingFile=argv[2];
  }else if (strcmp(argv[1],"-k")==0){
      key=atoi(argv[2]);
  }else if (strcmp(argv[1],"-m")==0){
      mode = atoi(argv[2]);
  }else{
       printf("invalid argument %s. Please re-run the program\n",argv[1]);
              exit(EXIT_FAILURE);
  }
  if (strcmp(argv[3],"-i")==0){
     mappingFile = argv[4];
  }else if (strcmp(argv[3],"-k")==0){
     key=atoi(argv[4]);
  }else if (strcmp(argv[3],"-m")==0){
     mode = atoi(argv[4]);
  }else{printf("invalid argument %s. Please re-run the program\n",argv[3]);
        exit(EXIT_FAILURE);
  }
  if (strcmp(argv[5],"-i")==0){
      mappingFile=argv[6];
  }else if (strcmp(argv[5],"-k")==0){
      key=atoi(argv[6]);
  }else if (strcmp(argv[5],"-m")==0){
      mode=atoi(argv[6]);
  }else{
      printf("invalid argument %s. Please re-run the program\n",argv[5]);
      exit(EXIT_FAILURE);
  }
  if (key >25){
      printf("You entered %d. Sorry, your key must be between 1 and 25. Re-                    run the program and try again\n", key);
      exit(EXIT_FAILURE);
  }
  if (mode !=1 &&mode!=2){
      printf("Unidentified mode. Run again!\n");
      exit(EXIT_FAILURE);
  }
  fp=fopen(mappingFile,"r");
  if  (fp==NULL){
      printf("Cannot open mapping file\n");
      exit(EXIT_FAILURE);
  }
  if (mode==1){
     printf("Enter the word you want to encrypt, upto 20 characters ");
     scanf("%20s", inputText);
     inputLength= strlen(inputText);

     encrypt(inputText, inputLength, key);
  }
  if (mode==2){
     printf("Enter the encrypted word you want to decrypt, upto 20 soace-     separated numbers. Put any letter at the end of your message ");
     i = 0;
     printf("Enter the encrypted word you want to decrypt, upto 20 soace-separated numbers. Put any letter at the end of your message ");
     i = 0;
     do{
         scanf("%d%c",&cipherText[i],&temp);
         i++;
      } while (temp ==' ');
      }
      inputLength=i;
      decrypt(cipherText, inputLength,key);
      }

void encrypt(char inputText[20], int inputLength, int key){
    int i,a,numb,character;
    char inputLetter,letter,String[20];
    for(a=0;a<=(inputLength-1);a = a+1){
       inputLetter = inputText[a];
       fopen(mappingFile,"r");
       while (fscanf(fp, "%c, %d", &letter, &numb)!=EOF){
           if (letter == inputLetter){
               String[a]=(numb-key+26);
               fclose(fp);
               break;
            }
       }
     }
     for (i=0;i<(inputLength);i++){
         character = String[i];
         printf("%d ",character);
     }
     printf("\n");
 }

void decrypt(int cipherText[20], int inputLength, int key){
     int i,a,numb,temp,inputNumber,character;
     char String[20],letter;

     for(a=0;a<=(inputLength-1);a = a+1){
         inputNumber = cipherText[a];
         i = 0;
         fopen(mappingFile,"r");
         temp=(inputNumber+key);
         if (temp>26){
            temp = temp -26;
         }
         while (fscanf(fp, "%c, %d", &letter, &numb)!=EOF){
            if (numb == temp){
                String[a] = letter;
                fclose(fp);
                break;
            }else{
               i++;
            }
         }
      }

      for (i=0;i<(inputLength);i++){
          character = String[i];
          printf("%c",character);
      }
      printf("\n");
  }

一个非常简单的加密/解密程序。从.csv文件中读取一个模式,该模式与输入密钥配对时提供加密解密信息。完美的工作做我需要的一切,打印出预期的输出。但我后来抛弃了分段错误,我不明白为什么

1 个答案:

答案 0 :(得分:0)

我发现了错误......一个不合适的地方。谢谢你的时间!