简体   繁体   中英

Getting segmentation fault(core dumped)

My program has to encrypt/decrypt the textfile but I'm getting segmentation fault(core dumped) when I do this:

./program 9999 input.txt output.txt

The program takes every character from the input file and converts it based on the passed key . It compiles fine when I compile in CodeBlocks and does not give any errors. Could smb tell me what's wrong with the code? Thanks!

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


//Checks if the input arguments are less than 2 (starting from 0)
//A user should enter 3 arguments so as not to reach this method
int badProgram(const char *const program){
    printf("The program is missing some of the files!");
    return -1;
}

//Encrypts the passed inputFile and
//Produces its output to the passed outputFile
//Key is also passed as the seeding number
int encryptFile(FILE *input, FILE *output){
    char c;
    char p;
    int r = 0;
    char p1 = 0;
    char c1 = 0;

    while((p = fgetc(input)) != EOF){
        r = rand() % 97;
        //change all displayable characters [0...96]
        if(p == 't'){
            p1 = 0;
        }
        else if(p == '\n'){
            p1 = 1;
        }
        else{
            p1 = p - 30;
        }

        c1 = p1 ^ r;//bitwise xor
        if(c1 == 0){
            c = 't';
        }
        else if(c1 == 1){
            c = '\n';
        }
        else{
            c = c1 + 30;
        }
        //Write
        fprintf(output, "%c", c);
    }

}

int main(int argc, char *argv[])
{
    //Check the number of the entered arguments
    if(argc < 2){
        return badProgram(argv[0]);
    }
    else{
        FILE *input;
        FILE *output;

        //Seed a number into Random Generator
        int key = *argv[0];
        srand(key);
        input = fopen(argv[1], "r");
        output = fopen(argv[2], "w");
        encryptFile(input, output);

    }

    return 0;
}

The **input.txt** looks like this:

Hello, World!

Bye!

Couple of things that are wrong with your code:

  • int key = *argv[0]; is most likely not doing what you think it does. What it actually does is the following:

    assign an ASCII value of the first character of the [0] argument (program name) to an int variable
    It is likely that what you intended to do there is:

    int key = atoi(argv[1]); // this converts "9999" into an int 9999

  • input = fopen(argv[1], "r"); opens a file named (in your case) "9999" for reading and fails. You never check for the error so this is causing a crash the moment you are trying to use the input FILE pointer. The fix is to use the argv[2]

  • Similarly you should be using argv[3] for the output file
  • Your encryptFile function must return a value as it is declared int (don't know why you want to return a value from it as you never use it)

Once you fix the above issues your program no longer crashes

Update

A bit of explanation for the above issues and general info:

  • The argv lists all the input parameters as strings ( char* ) where the first ( [0] ) argument is the executable name and is not your first argument "after" the program name
  • One should always check the results of file operations as they are quite likely to fail during "normal" program operation
  • C/C++ doesn't "automatically" convert a string into an int (or a double , for that matter) but provides a whole set of functions to deal with numbers' parsing. Some examples of those functions are: 'atoi', 'atol', 'atof'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM