Find the answer to your Linux question:
Results 1 to 3 of 3
Hello All, My first venture into trial by forum, please be tolerant While trying to understand the delights of shared memory the following code gives a segmentation fault error with ...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Location
    On the bay, Morecambe Bay.
    Posts
    15

    gets() versus fgets()

    Hello All,
    My first venture into trial by forum, please be tolerant

    While trying to understand the delights of shared memory the following code gives a segmentation fault error with fgets(), however, with gets() (naughty I know) the program works fine.

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

    char s[20], *rs;
    struct bmds *p_bmds; /* Shared memory structure */

    /* Shared memory set-up and working across processes */

    while( p_bmds->id == 1 ){
    printf("\n");
    printf("Set flag? ");
    rs = fgets(s, 20, 0);
    printf("\n");
    n = atoi(s);
    p_bmds->flags = n;
    printf("FLAGS %d \r",p_bmds->flags);
    printf("Set gauge? ");
    rs = fgets(s, 20, 0);
    printf("\n");
    g = atof(s);
    p_bmds->gauge[0] = g;
    printf("FLAGS %d \r",p_bmds->flags);
    }

    Suggestions please??

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Erm, you're passing in 0 (NULL) for the stream to read from. It should be "stdin".
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Jan 2009
    Location
    On the bay, Morecambe Bay.
    Posts
    15
    Thank you.
    I am guilty here of mixing the file descriptor for stdin (0) and FILE *stream.
    Another case of "RTFM" {Read The Flipin Man-pages}, properly!!!!!

    Thanks again for the speedy reply, very much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...