I have written this part of code which opens a txt file to write the results(tokens) which strtok gives me every 10 seconds from a buffer. This buffer reads from a serial device.
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); I have this kind of results in my 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 Every number 7 or 10 returns 4 numbers as you can see from the results. I would like to create the following: if number=7 then
20.83 51.79 11.05 4.85 0 0 0 0 if number=7 then
0 0 0 0 21.83 53.79 12.05 3.85 I mean I want to have an array of zeros. Then if the number=7 put the values in the positions 0-3, clean the array, if the number=10 then put the values in positions 4-7. Any good idea?
memsetfunction.memsetin your code. Perhaps you can usememcpy? I mean collect the four numbers in an array and latter copy them to array of zeroes starting from desired position usingmemcpy.