1

I am fairly new to coding in C and I am working on a project where I have to input a file into an array but when I run the code it gives me a seg fault. Could someone please look over my code to figure out where I am going wrong? Is it because I am not allocating enough space? To me =, it makes logical sense but im not sure where the seg fault is coming from.

#define MAX 1000

int checkSpelling(const char *input, const char *dict);
int searchReplace(const char *input, const char *dict);
int save(const char *input, const char *output);

int main (int argc, char **argv){
    int choice = 0;

    if(argc != 4){
            printf("Not enough input arguments in the command line.\n");
            exit(1);
    }

    do{
            printf("Menu\n");
            printf("1. Check the spelling for the file using the dictionary.\n");
            printf("2. Search and replace a given string in the inputed file.\n");
            printf("3. Save the modified file to the output file.\n");
            printf("4. Exit the program.\n");
            scanf("%d", &choice);

            switch(choice){
                    case 1:
                            checkSpelling(argv[1], argv[3]);
                            break;
                    case 2:
                            searchReplace(argv[1], argv[3]);
                            break;
                    case 3:
                            save(argv[1],argv[2]);
                            break;
                    case 4:
                            choice = 4;
                            break;
                    default:
                            printf("Input is invalid");
                            break;
            }

     }while (choice != 4);
return 0;
}

int checkSpelling(const char *input, const char *dict){

    FILE *inputFile;
    FILE *dictFile;
    char temp[MAX] = {0}, dicttemp[MAX];
    int i = 0;
    char *pInput, *pDict;

    printf("Checking the spelling of the inputed file.\n");
    printf("Opening the inputed file...\n");
    if((inputFile = fopen(input, "rt")) == NULL){
            printf("Cannot open file\n");
            exit(1);
    }

    printf("Opening the dictonary...\n");
    if((dictFile = fopen(dict, "rt")) == NULL){
            printf("Cannot open dictionary\n");
            exit(1);
    }

    pInput = (char *)malloc(MAX * sizeof(char));
    pDict = (char *)malloc(MAX * sizeof(char));

    //putting the information from input file into one long array called temp
    while(feof(inputFile)){
            temp[i++] = fgetc(inputFile);
    }
    temp[i] = '\0';

    i = 0;
    for(i = 0; i < MAX; i++){
       printf("%c", temp[i]);
    }
    printf("\n");
    fclose(dictFile);
    fclose(inputFile);
return 0;
}

 int searchReplace(const char *input, const char *dict){

return 0;
 }

//Moves the data from the input file into the output file and saves it
int save(const char *input, const char *output){
    FILE *inputFile;
    FILE *outputFile;

    if((inputFile = fopen(input, "rt")) == NULL){
            printf("Unable to open input file.\n");
            exit(1);
    }

    if((outputFile = fopen(output, "wt"))== NULL){
            printf("Unable to open output file.\n");
            exit(1);
    }

    fclose (inputFile);
    fclose (outputFile);
return 0;
}
sarah
  • 59
  • 1
  • 5
  • Have you tried running your program under `gdb` (a debugger) to see where the crash is occurring? – Sean Bright Mar 25 '19 at 17:16
  • If your `MAX` is very large, then allocating it on the stack will make the pogram abort due to stack overflow. – Paul Ogilvie Mar 25 '19 at 17:18
  • Can you include your definition of MAX as well as what headers you include. – Patrick Mar 25 '19 at 17:21
  • I have decreased the size of MAX from 1000000 to 1000 but now it isnt printing out anything. Is the logic of the problem wrong? – sarah Mar 25 '19 at 17:25
  • With value 1000 it seems `MAX` is not the problem. – Paul Ogilvie Mar 25 '19 at 17:26
  • 1
    There is no check in `while(feof(inputFile)){ temp[i++] = fgetc(inputFile); }` that would prevent your program from reading more that what fits into `temp`. The condition `while(feof(...))` is wrong. In theory it would be `while(!feof(...))`, but even this is wrong. Instead of checking `feof` you should check the return code of `fgetc`. `feof` can be used **after** `fgetc` or `fgets` has returned a value that indicates EOF or error if you want to distinguish between these two. Better than reading single characters with `fgetc` might be reading complete lines with `fgets`. – Bodo Mar 25 '19 at 17:28
  • 1
    `while(feof(inputFile))` is wrong (and [so is `while(!feof(inputFile))`](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong), incidentally.) – molbdnilo Mar 25 '19 at 17:28
  • Is 1000 characters enough to hold the contents of the file? – Mitch Mar 25 '19 at 17:28
  • @MitchelPaulin No it isnt...the file is about 1000 words – sarah Mar 25 '19 at 17:29
  • Then you should check to make sure `i < MAX` before you attempt to write – Mitch Mar 25 '19 at 17:30
  • @molbdnilo to fix that should i use while(inputFile != EOF)? – sarah Mar 25 '19 at 17:30
  • 2
    Something like `while(((ch = fgetc(inputFile)) != EOF) && (i < MAX-1)) { temp[i++] = ch; }`. You should [edit] your question to show how the contents of the input file looks like. – Bodo Mar 25 '19 at 17:34
  • @Bodo that worked! Thanks so much! – sarah Mar 25 '19 at 17:38

0 Answers0