在Debian中使用C程序,我需要访问一个在其末尾有数字的目录,偶尔会有变化。从命令提示符访问时,我可以选项卡完成或使用*,如何使用fopen或其他方法从C程序执行此操作?
pwm = fopen("/sys/devices/ocp.3/pwm_test_P8_19.15/duty // this is the changing directory
pwm = fopen("/sys/devices/ocp.3/pwm_t*/duty // this did not work
答案 0 :(得分:0)
使用stdio.h,stdlib.h,unistd.h
int k = 0;
char pwm_path[100];
for (k = 14; k < 20; k++)
{
sprintf( pwm_path, "/sys/devices/ocp.3/pwm_test_P8_19.%d/period", k );
puts(pwm_path); //debug
if (access( pwm_path, F_OK ) == 0) // if it finds path, then = 0
{
//printf("Files does exists, %d\n", k); // debug
pwm = fopen( pwm_path, "w" );
fseek(pwm,0,SEEK_SET);
fprintf(pwm,"20000000"); // pulse period in uS
fflush(pwm); // flush free up memory
break; // break out of loop once found
}
}