segmentation fault while readin gpio pins

Hi, I am new to this group. Following is a code I tried from Derek Malloy’s videos, to read the gpio pin values. When I run this program I get “Segmentation fault”. I can’t make out how it happens. Can anybody help me out?

`
#include <stdio.h>
#include <string.h>
#define MAX 128
int readButton(int);

int main()
{
int i = 1000;
while(i–)
readButton(60);
return 0;
}

int readButton(int pin)
{
char setValue[4], GPIOPin[4], GPIODirection[MAX], GPIOValue[MAX];
FILE *GPIOHandler;
int i;
sprintf(GPIOPin, “%d”, pin);
sprintf(GPIODirection, “/sys/class/gpio/gpio%s/direction”, GPIOPin);
sprintf(GPIOValue, “/sys/class/gpio/gpio%s/value”, GPIOPin);
if ((GPIOHandler = fopen("/sys/class/gpio/export", “ab”)) == NULL)
{
fprintf(stderr, “Failed to export the GPIO\n”);
return 1;
}
strcpy(setValue, GPIOPin);
fwrite(GPIOPin, sizeof(char), 2, GPIOHandler);
fclose(GPIOHandler);
if ((GPIOHandler = fopen(GPIODirection, “rb+”)) == NULL)
{
fprintf(stderr, “Failed to set the direction\n”);
return 1;
}
strcpy(setValue, “in”);
fwrite(&setValue, sizeof(char), 3, GPIOHandler);
fclose(GPIOHandler);

if ((GPIOHandler = fopen(GPIOValue, “rb+”)) == NULL)
{
fprintf(stderr, “Failed to set read file\n”);
return 1;
}
fread(&setValue, sizeof(char), 1, GPIOHandler);
fclose(GPIOHandler);
printf(“The value of the pin is %s\n”, setValue[0]);
if ((GPIOHandler = fopen("/sys/class/gpio/unexport", “ab”)) == NULL)
{
fprintf(stderr, “Failed to unxport GPIO \n”);
return 1;
}
strcpy(setValue, GPIOPin);
fwrite(&setValue, sizeof(char), 2, GPIOHandler);
fclose(GPIOHandler);
return 0;
}

`

Compile the code with 'gcc -g -o readButton readButton.c' , and run it
with 'gdb readButton'. You will get a gdb prompt, so just give it the
'r' command. When you get the SEGV, gdb will stop and show you the
line it happened in, whereas you will be able to examine variables in
the program, etc.

Hi,

Hi, I am new to this group. Following is a code I tried from Derek Malloy's
videos, to read the gpio pin values. When I run this program I get
"Segmentation fault". I can't make out how it happens. Can anybody help me
out?

#include <stdio.h>
#include <string.h>
#define MAX 128
int readButton(int);

int main()
{
        int i = 1000;
        while(i--)
                readButton(60);
        return 0;
}

int readButton(int pin)
{
        char setValue[4], GPIOPin[4], GPIODirection[MAX], GPIOValue[MAX];
        FILE *GPIOHandler;
        int i;
        sprintf(GPIOPin, "%d", pin);
        sprintf(GPIODirection, "/sys/class/gpio/gpio%s/direction", GPIOPin);
        sprintf(GPIOValue, "/sys/class/gpio/gpio%s/value", GPIOPin);
        if ((GPIOHandler = fopen("/sys/class/gpio/export", "ab")) == NULL)
        {
                fprintf(stderr, "Failed to export the GPIO\n");
                return 1;
        }
        strcpy(setValue, GPIOPin);
        fwrite(GPIOPin, sizeof(char), 2, GPIOHandler);

anything bad with
  fprintf(GPIOHandler,"%d",pin) ?

re,
wh

Hi walter,

Hi, I am new to this group. Following is a code I tried from Derek Malloy’s
videos, to read the gpio pin values. When I run this program I get
“Segmentation fault”. I can’t make out how it happens. Can anybody help me
out?

…snip…

int readButton(int pin)
{
char setValue[4], GPIOPin[4], GPIODirection[MAX], GPIOValue[MAX];
FILE *GPIOHandler;
int i;
sprintf(GPIOPin, “%d”, pin);
sprintf(GPIODirection, “/sys/class/gpio/gpio%s/direction”, GPIOPin);
sprintf(GPIOValue, “/sys/class/gpio/gpio%s/value”, GPIOPin);
if ((GPIOHandler = fopen("/sys/class/gpio/export", “ab”)) == NULL)
{
fprintf(stderr, “Failed to export the GPIO\n”);
return 1;
}
strcpy(setValue, GPIOPin);
fwrite(GPIOPin, sizeof(char), 2, GPIOHandler);

anything bad with
fprintf(GPIOHandler,"%d",pin) ?

That would probably be better in this situation (less buffer manipulation, and fprintf to a file won’t cause any buffer overruns). Similarly for writing out the direction below.

The proble was %s, %c works perfect.
Thank you all