c - 运行时出现分段核心转储错误


#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 programn",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 programn",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 programn",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 againn", 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 filen");
      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文件中读取架构,当与输入密钥配对时,可提供加密解密信息。完美地工作,可以满足我需要的一切,打印出预期的输出。但是我之后被转储了分段错误,我不知道为什么

我发现了错误...不合适的括号。谢谢你的时间!

相关内容

  • 没有找到相关文章

最新更新