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 ...
- 01-14-2009 #1Just 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??
- 01-14-2009 #2
Erm, you're passing in 0 (NULL) for the stream to read from. It should be "stdin".
DISTRO=Arch
Registered Linux User #388732
- 01-14-2009 #3Just 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.


Reply With Quote