我已经编写了这部分代码,它打开一个txt文件来写入strtok每10秒从缓冲区给我的结果(令牌)。此缓冲区从串行设备读取。
while(fgets(buff, sizeof(buff), fp) != NULL)
{
fputs(buff,stdout);
FILE *ft = fopen("/home/pi/Desktop/data.txt","a+");
struct tm *tp;
time_t t;
char s[80];
t = time(NULL);
tp = localtime(&t);
strftime(s, 80, "%d/%m/%Y %H:%M:%S", tp);
char *pos = strchr(buff,'N');
if (pos)
{
ptr = strtok(buff, "Nodo_,=:V()");
i = 0;
while (ptr != NULL)
{
if (i == 0)
strcat(number, ptr);
if (i == 2)
strcat(temp, ptr);
if (i == 4)
strcat(hr, ptr);
if (i == 6)
strcat(dw, ptr);
if (i == 8)
strcat(vcc, ptr);
ptr = strtok(NULL, "Nodo_,=:V()");
i++;
}
printf("Results: %s, %s, %s, %s, %s\n", number, temp, hr, dw, vcc);
char (*table1[1][5])[6] = {{&number, &temp, &hr, &dw, &vcc}};
for(int i = 0; i<1; i++)
{
fprintf(ft,"%s ",s);
for(int j = 0; j<5; j++)
fprintf(ft,"%s ",table1[i][j]);
}
fprintf(ft,"\n");
buff[sizeof(buff)-1] = '\0';
memset(nodo, 0, sizeof(number));
memset(temp, 0, sizeof(temp));
memset(hr, 0, sizeof(hr));
memset(dw, 0, sizeof(dw));
memset(vcc, 0, sizeof(vcc));
printf("\n");
}
fclose(ft);
}
close_port(fd);我的txt中有这样的结果。
19/06/2013 13:16:34 7 20.83 51.79 11.05 4.85
19/06/2013 13:16:34 10 21.83 53.79 12.05 3.85正如您从结果中看到的,每个数字7或10返回4个数字。我想创建以下内容:如果是number=7,那么
20.83 51.79 11.05 4.85 0 0 0 0 如果为number=7,则
0 0 0 0 21.83 53.79 12.05 3.85我的意思是我想要一个零的数组。然后,如果number=7将值放在位置0-3,则清理数组;如果number=10将值放在位置4-7,则清理数组。有什么好主意吗?
发布于 2013-06-20 15:47:03
也许你应该使用calloc..。看看这个http://www.codingunit.com/c-reference-stdlib-h-function-calloc
https://stackoverflow.com/questions/17207288
复制相似问题