0

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?

3
  • 2
    The question is not clear. If setting an array with zeroes is the problem, then you can use memset function. Commented Jun 20, 2013 at 7:38
  • No I know that with memset I can set an array with zeros, my question is how can I do the above implementation with the positions as I described Commented Jun 20, 2013 at 7:47
  • Yes, now I see you are using memset in your code. Perhaps you can use memcpy? I mean collect the four numbers in an array and latter copy them to array of zeroes starting from desired position using memcpy. Commented Jun 20, 2013 at 8:25

1 Answer 1

1

Maybe you should use calloc... have a look at this http://www.codingunit.com/c-reference-stdlib-h-function-calloc

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.