我昨天写了一个 C code
,效果很好。现在,我想对文件进行一些更改,因此我只需在新文件中复制此代码即可。新文件的编译就可以了,但是当我尝试运行可执行文件时,我会得到segmentation fault
。但是我以前的代码正在工作。最初,我认为这是一个权限问题,因此我使用chmod 777
更改可执行文件的许可。但是我仍然得到segmentation fault
。这是我的代码: -
#include <stdio.h>
#include <time.h>
void pauseSec(int sec);
int main() {
FILE *io,*iodir,*ioval,*iodir_S,*ioval_S,*io_S,*iodir_P,*ioval_P,*io_P;
io = fopen("/sys/class/gpio/export", "w");
fseek(io,0,SEEK_SET);
fprintf(io,"%d",15);
fflush(io);
iodir = fopen("/sys/class/gpio/gpio15/direction", "w");
fseek(iodir,0,SEEK_SET);
fprintf(iodir,"out");
fflush(iodir);
while(1)
{
fprintf(ioval,"%d",1);
fflush(ioval);
pauseSec(1);
fprintf(ioval,"%d",0);
fflush(ioval);
pauseSec(1);
}
fclose(io);
fclose(iodir);
fclose(ioval);
return 0;
}
void pauseSec(int sec) {
time_t now,later;
now = time(NULL);
later = time(NULL);
while((later - now) < (double)sec)
later = time(NULL);
}
我想知道为什么在两个不同的文件中写入相同的代码时,相同的代码行为会有所不同。这样做的原因是什么?
有问题,忘记初始化 ioval
。正确的代码在这里: -
#include <stdio.h>
#include <time.h>
void pauseSec(int sec);
int main() {
FILE *io,*iodir,*ioval,*iodir_S,*ioval_S,*io_S,*iodir_P,*ioval_P,*io_P;
io = fopen("/sys/class/gpio/export", "w");
fseek(io,0,SEEK_SET);
fprintf(io,"%d",15);
fflush(io);
iodir = fopen("/sys/class/gpio/gpio15/direction", "w");
fseek(iodir,0,SEEK_SET);
fprintf(iodir,"out");
fflush(iodir);
ioval = fopen("/sys/class/gpio/gpio15/value", "w");
while(1)
{
fprintf(ioval,"%d",1);
fflush(ioval);
pauseSec(1);
fprintf(ioval,"%d",0);
fflush(ioval);
pauseSec(1);
}
fclose(io);
fclose(iodir);
fclose(ioval);
return 0;
}
void pauseSec(int sec) {
time_t now,later;
now = time(NULL);
later = time(NULL);
while((later - now) < (double)sec)
later = time(NULL);
}