简体   繁体   中英

Why do I get “Segmentation fault (core dumped)”

I am new to C programming, and I'm trying to write a program in linux.

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



 char num, fileName[260];
   int numCount, inp;
   FILE *fp, *nf;


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




   printf( "1. New file\n" );
   printf( "2. Display file\n" );
   printf( "3. Edit file\n" );
   printf( "4. Exit\n" );
   scanf( "%d", &inp );



   switch (inp){
    case 1:            
        displayFile();
       break;
    case 2:            
        printf("\n");
        break;
    case 3:            
        printf("\n");
        break;
    case 4:            
        printf("\n");
        break;
    default:            
        printf("\n");
        break;
   }




   fclose(fp);
   return 0;
}



int displayFile(void){

    printf("Enter the name of file. \n");
    scanf("%s",fileName);
    fp = fopen(fileName,"r");


    if(!fp){

        printf("Error! Cannot open file \n");
        exit(1);
   }

    else{
        printf("The content of %s are :\n", fileName);

        while( ( num = fgetc(fp) ) != EOF ) //End-of-file
            numCount++;
            printf("%c",num);

    }
        printf("There are %i numbers in file:\n", numCount);

}

This is what happens when I run the program: It starts, gives me the case selection, I select "Display file", it gives me the "enter name of file". However I do not get choice to write before the "core dumped" error message appears.

When you select 2. Display file , fp is not assigned and its value is NULL since it is global variable which is not explicitly initialized.

fclose seems to crush when NULL is passed.

For example,

#include <stdio.h>

FILE *fp;


int main(void){
   fclose(fp);
   return 0;
}

emitted Segmentation Fault on Wandbox .

I think you should close the file not in main function but in displayFile function.

Also, you shouldn't have too many global variables: you should make some of them local.

Your program is very wrong in the approach, you don't need to declare a single global variable in your program.

Among the problems there are:

  1. You don't give a declaration ( prototype ) for displayFile() that could cause undefined behavior , and if you had compiler warnings enabled the compiler would warn about implicit function declaration .

  2. Using global variables makes your code messy and potentially buggy.

  3. You open the file in one function, and close it in another funcion and also you don't check for NULL before calling fclose() and since passing NULL to fclose() is undefined behavior it seems to be the actual problem.

    Openning the file in one funcion and closing it in antoher, is conceptually wrong. It's made possible in your program because of point 2.

    Global variables are dangerous because it's hard to track their state, you can modify it in a function, and miss that when using it in another. Moreover, global variables are very rarely needed.

  4. You never check for scanf() 's return value, in the first scanf() it's very dangerous because it would cause undefined behavior when reading the inp .


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

int displayFile(void);
int main(int argc, char *argv[])
{
    int inp;

    printf( "1. New file\n" );
    printf( "2. Display file\n" );
    printf( "3. Edit file\n" );
    printf( "4. Exit\n" );

    if (scanf("%d", &inp) != 1)
        return -1;
    switch (inp)
    {
    case 1:
        displayFile();
        break;
    case 2:
        printf("\n");
        break;
    case 3:
        printf("\n");
        break;
    case 4:
        printf("\n");
        break;
    default:
        printf("\n");
        break;
    }
    return 0;
}

int displayFile(void)
{
    FILE *fp;
    char fileName[100];
    int numCount;
    int num;

    printf("Enter the name of file. \n");
    scanf("%99s", fileName);

    fp = fopen(fileName, "r");
    if (fp != NULL)
    {
        printf("The content of %s are :\n", fileName);
        while (( num = fgetc(fp)) != EOF)
            numCount++;
        printf("%c", num);
        printf("There are %i numbers in file:\n", numCount);
        fclose(fp);

        return 0;
    }
    return -1;
}

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