繁体   English   中英

如何在 C 程序中从终端读取输入

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM