How to write to file in C -
i have program working on start , stop servo. can issue following command command line , works. echo 2=120 > /dev/servoblaster
start servo in motion. have following program
#include <stdio.h> #include <stdlib.h> int main(void) { file *fp; fp = fopen("/dev/servoblaster", "w"); if (fp == null) { printf("error opening file\n"); exit(0); } fprintf(fp, "2=120"); fclose(fp); fflush(fp); return 0; }
but when execute nothing happens, when try echo 2=120 > /dev/servoblaster
command bad input: 2=1202=120
if repeat same echo 2=120 > /dev/servoblaster
command work again. if try , execute above program 3 times output when try execute echo
command output bad input 2=1202=1202=120 2=120
me seems file not finished being written in program. can point out if missing something?
you need add newline after command, echo
does:
fprintf(fp, "2=120\n");
presumably, servo's driver waits until sees newline before acting on command.
Comments
Post a Comment