Advertisement
slabua

popen() and substrings

Oct 18th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1. char *command = "dht";
  2. // The command "dht" returns a string in the form "xx.x xx.x"
  3.  
  4. FILE *fd = popen(command, "r");
  5. char buffer[10];
  6. fgets(buffer, 10, fd);
  7. pclose(fd);
  8.  
  9. char temp[5] = "";
  10. char humid[5] = "";
  11.  
  12. strncpy(temp, buffer, 4);
  13. temp[4] = '\0';
  14. strncpy(humid, buffer+5, 4);
  15. humid[4] = '\0';
  16.  
  17. char tempInfo[15];
  18. sprintf(tempInfo, "Temp: %s C", temp);
  19.  
  20. char humidInfo[15];
  21. sprintf(humidInfo, "Hum: %s %%", humid);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement