[英]how can I read input from terminal in a C program
我有一個連接到熱敏電阻的電路並使用系統(命令)我可以讓它將值讀入終端,但我無法讓它正確地將值發送到我的 C 程序。
我正在向終端發送代碼以運行,該代碼在終端中返回一個值。 我想將此值帶入我的 C 程序
這是我的代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for Log
float Vo; //voltage out
float Ro = 5000; //value of resistance at 25C in the thermistor
float R ; //value of fixed resistor
float B = 3977; //Beta constant
float T0 = 298.15; //25 degrees C in Kelvin
//float logR2, R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07; // Ste$
int main(){
char command[50];
//get the Analog value
sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw"$
Vo = system(command);
printf("Analog Reading: %d\n", Vo);
return 0;
}
一個示例 output 將是
3973
模擬讀數:4319452
4319452 來自哪里? 我想讓它讀 3973
閱讀評論后,我嘗試通過以下方式使用 fopen
//#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for Log
float Vo; //voltage out
float Ro = 5000; //value of resistance at 25C in the thermistor
float R ; //value of fixed resistor
float B = 3977; //Beta constant
float T0 = 298.15; //25 degrees C in Kelvin
//float logR2, R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07; // Steinhart-Hart and Hart Coefficients
int main(){
FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio\\:device0/in_voltage0_raw","r");
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c", c);
}
fclose(fp);
//Vo[i] = system(command);
printf("Analog Reading: %d\n", c);
return 0;
}
我明白了
分段故障
運行代碼后
我通過以下方式編輯了代碼
sprintf(command, "cd /sys/bus/iio/devices/iio:device0 && cat in_voltage0_raw"$
運行它后,我得到以下 output
3 9 6 8
模擬讀數:▒
它會正確讀取 output 一次但在第二次
printf("Analog Reading: %d\n", c);
它給了我
模擬讀數:▒
到目前為止的完整代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for Log
int Vo; //voltage out
float Ro = 5000; //value of resistance at 25C in the thermistor
float R ; //value of fixed resistor
float B = 3977; //Beta constant
float T0 = 298.15; //25 degrees C in Kelvin
//float logR2, R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07; // Steinhart-Hart and Hart Coefficients
int main(){
FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio:device0/in_voltage0_raw","r");
if ( fp == NULL ){
perror("Error: ");
return(-1);
}
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c ", c);
// Vo = c;
}
fclose(fp);
//Vo[i] = system(command);
printf("Analog Reading: %c\n", c);
return 0;
}
我通過讀取每個字符並將其放入數組中解決了這個問題
這里是工作代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for Log
char Vo[10]; //voltage out
float Ro = 5000; //value of resistance at 25C in the thermistor
float R ; //value of fixed resistor
float B = 3977; //Beta constant
float T0 = 298.15; //25 degrees C in Kelvin
//float logR2, R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07; // Steinhart-Hart and Hart Coefficients
int main(){
FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio:device0/in_voltage0_raw","r");
if ( fp == NULL ){
perror("Error: ");
return(-1);
}
int i=0;
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c ", c);
if (c != '\0'){
Vo[i] = c;
++i;
}
// Vo = c;
}
fclose(fp);
//Vo[i] = system(command);
printf("Analog Reading: %s\n", Vo);
return 0;
}
Output:
3 9 7 0
模擬讀數:3970
看起來您假設系統 function 中的 output 是您的價值; 它不是。 system()
的返回值是 shell 的退出狀態,它是int
而不是 float。
我不確定返回碼是什么,但您沒有正確轉換,我很驚訝您沒有收到如下編譯警告:
t.c:22:36: warning: format specifies type 'int' but the argument has type 'float' [-Wformat]
printf("Analog Reading: %d\n", Vo);
~~ ^~
%f
1 warning generated.
基本上,這意味着您正在獲取一個int
並且在沒有適當的強制轉換或轉換的情況下將其放置為float
。 它實際上不是您想要的數據。 您想要文件的內容。
您可能想要做的是將/sys/bus/iio/devices/iio\\:device0/in_voltage0_raw
直接作為字符串讀取,然后將其轉換為適當的數值。
從技術上講,您要做的是通過返回值從 shell 讀取標准輸出,這對於您正在使用的 function 是不可能的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.