浮到弦

时间:2014-05-29 18:02:15

标签: c string char int

必须有一种更简单的方法来做到这一点。我想要做的是将一组int和floats转换为char,然后将它们组合成一个char。它没有像预期的那样构建字符串str  写在C.

char *str_W1;
char *str_W2;
char *str_W3;
char *str_W4;
char *str_W5;
char *str_W6;
char *str_VBAT;
char *str_day;
char *str_month;
char *str_year;
char *str_hour;
char *str_minute;

sprintf(str_day, "%d", time.day);       //casting int to string
sprintf(str_month, "%d", time.month);   //casting int to string
sprintf(str_year, "%d", time.year);     //casting int to string
sprintf(str_hour, "%d", time.hour);     //casting int to string
sprintf(str_minute, "%d", time.minute); //casting int to string
sprintf(str_W1, "%.2f", value_w1);      //casting float to string
sprintf(str_W2, "%.2f", value_w2);      //casting float to string
sprintf(str_W3, "%.2f", value_w3);      //casting float to string
sprintf(str_W4, "%.2f", value_w4);      //casting float to string
sprintf(str_W5, "%.2f", value_w5);      //casting float to string
sprintf(str_W6, "%.2f", value_w6);      //casting float to string 
sprintf(str_VBAT, "%.2f", value_vbat);  //casting float to string

char *str;
/* Building One String */
strcpy (str, str_day );
strcat (str, "/");
strcat (str, str_month);
strcat (str, "/");
strcat (str, str_year);
strcat (str, ",");
strcat (str, str_hour);
strcat (str, ":");
strcat (str, str_minute);
strcat (str, ",");
strcat (str, str_W1);
strcat (str, ",");
strcat (str, str_W2);
strcat (str, ",");
strcat (str, str_W3);
strcat (str, ",");
strcat (str, str_W4);
strcat (str, ",");
strcat (str, str_W5);
strcat (str, ",");
strcat (str, str_W6);
strcat (str, ",");
strcat (str, str_VBAT);

3 个答案:

答案 0 :(得分:2)

首先,您需要为指针分配内存。

其次,正如Kerreck SB指出的那样,你可能想看看strftime,就像这样

    //Format time
    strftime(time,10,"%T",localtime(&curtime));

答案 1 :(得分:2)

您可以将sprintf(或更好的snprintf)用于多个格式字符。这消除了对strcpystrcat的需求。另外,不要忘记为字符串实际分配一个数组。如果要构建一个小字符串,则可以预先分配一个固定大小的数组,并使用以下代码填充它:

char s[256];
snprintf(s, sizeof s, "%d/%d/%d,%d:%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f",
         time.day, time.month, time.year, time.hour, time.minute,
         value_w1, value_w2, value_w3, value_w4, value_w5, value_w6,
         value_vbat);

// proceed working with s

答案 2 :(得分:2)

你真的应该通过Kernighan& Ritchies C编程语言(第2版)。

但是在回答您的问题时,您使用strftime()中的time.hsprintf()中的stdio.h

由于您看起来像是使用非标准时间结构,因此您需要一个映射功能,如下所示:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

typedef struct moment_in_time
{
  int year   ; // year   (e.g. 2014)
  int month  ; // month  (1-12)
  int day    ; // day    (1-31)
  int hour   ; // hour   (0-23)
  int minute ; // minute (0-59)
} MOMENT_IN_TIME ;


void convert_moment_to_struct_tm( MOMENT_IN_TIME *m , struct tm *p )
{
  struct tm  src  = { 0      , m->minute    , m->hour        ,
                      m->day , m->month - 1 , m->year - 1900 ,
                      0  , 0 , // day-of-week and day-of-year are ignored by mktime()
                      -1 ,      // and we've got no idea if Daylight Savings (Summer) Time is in effect or not
                    } ;
  time_t     tx = mktime(&src) ;
  struct tm *tm = localtime( &tx ) ;

  memcpy( p , tm , sizeof(struct tm) ) ;

  return ;
}

一旦你有了struct tm,你应该可以这样写:

char *format_time( struct tm *tm , double w1, double w2, double w3, double w4, double w5, double w6, double vbat)
{
  size_t     bufl = 8192 * sizeof(char) ;
  char      *buf  = calloc(8192, sizeof(char)) ;
  char      *p    = buf ;

  p += strftime( p , bufl , "%d/%m/%Y, %H:%M, " , tm ) ;
  p += sprintf(  p , "%.2f, %.2f, %.2f, %.2f, %.2f, %.2f" ,
         w1 , w2 , w3 , w4 , w5 , w6
         ) ;
  p += sprintf(p, ", %.2f" , vbat ) ;

  return buf;
}

然后你可以这样说:

int main(  int argc , char *argv[] )
{
  MOMENT_IN_TIME moment = { 2014 , 05 , 26 , 15 , 55 , } ;
  struct tm      tm     ;

  convert_moment_to_struct_tm( &moment , &tm ) ;

  char *formatted = format_time( &tm , 1.234 , 2.345 , 3.456 , 4.567 , 5.678 , 6.789 , 7.890 ) ;

  fwrite( formatted , sizeof(*formatted) , strlen(formatted) , stdout );
  free(formatted);

  return 0;
}

并制作

26/05/2014, 15:55, 1.23, 2.35, 3.46, 4.57, 5.68, 6.79, 7.89