tomatediabolik
tomatediabolik

Reputation: 111

Segmentation fault 11 while creating a file

I'm working on an easy project but I've encountered an error. I'm coding in Unix and executing the code with the terminal.

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int atis;
    char *weather;

    //Création du fichier ATIS
    if((atis = creat("atis", 0666)) == -1)
    {
        printf("Error while creating the ATIS file\n");
        exit(-1);
    }

    //Ouverture du fichier ATIS
    if((atis = open("atis", 0666)) == -1)
    {
        printf("Permission denied\n");
        exit(-1);
    }

    //Mise à jour du fichier ATIS
    printf("OK or KO for a take-off? ");
    gets(weather);
    if(write(atis, weather, sizeof(weather))==-1)
    {
        printf("Write error\n");
        exit(-1);
    }


    close(atis);
    return 0;
}**

The error is a segmentation fault 11.

Thank you in advance! (and sorry for my English, it's really bad ^^)

Upvotes: 0

Views: 399

Answers (5)

Mike
Mike

Reputation: 49473

You never assign any memory to your pointer: char *weather;

From the man page:

char *gets(char *s);
gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF,

So you need a buffer to store it into:

char weather[10];

or

char *weather;
weather = malloc(10);

would do it if 10 is sufficient. That leads to the other point, again, from the man page:

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

ex:

fgets (weather, 10 , stdin);

Upvotes: 2

hmjd
hmjd

Reputation: 122001

weather is an unitialized char* when first used at the following call:

gets(weather);

meaning gets() will be attempting to write to memory it should not, causing the segmentation fault. Allocate memory for weather or use an array:

char weather[128];

In the subsequent call to write() use strlen(weather) instead of sizeof(weather) to only write the characters that were read (and to correctly handle the case where weather is a char* and not a char[]).

Additionally, see Why is the gets function so dangerous that it should not be used?. Use fgets() or possibly scanf() with length specifier instead.

Upvotes: 5

MOHAMED
MOHAMED

Reputation: 43568

char *weather;

and then

gets(weather);

that cause a segmentation fault. the weather should be pointed to a memory space:

  • staic

    char weather[100]

  • or dynamic

    char *weather = malloc(100*sizeof(char));

Upvotes: 0

matekm
matekm

Reputation: 6030

You do gets(weather) on a pointer that doesn't point to allocated memory.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409432

The problem is these two lines:

char *weather;

and

gets(weather);

The first declares weather to be a pointer, but it's left uninitialized (i.e. points to a seemingly random location). The second line writes to what weather points to, which means you write to some random location.

Declare weather as an array, and use fgets instead:

char weather[64];

/* ... */

fgets(weather, sizeof(weather), stdin);

Upvotes: 2

Related Questions