PDA

View Full Version : [C] time.h -> non aggiorna il tempo..xke'??!


andrew1988
03-04-2009, 16:31
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#define CPUTEMP "/proc/acpi/thermal_zone/THRM/temperature" /* path to the thermal cpu sensor's file */
#define TMP "/tmp/temp" /* path to the temporary file to storage datas */

int main()
{

FILE *ifp; /* referred to CPUTEMP */
FILE *ofp; /* referred to TMP */
char what[9];
int a=1000000; /* actual cpu temperature value */
int b=100; /* min. cpu temp. */
int c=0, i; /* max. cpu temp. */
time_t actual=0 /* time now */;
time_t mint=0 /* time at min temp */;
time_t maxt=0 /* time at max temp */;
time_t gotime=0 /* time at program start */;
time_t fGotime=0 /* formatted gotime */;
time_t fActual=0 /* formatted actual time */;
time_t fMint=0 /* formatted min time */;
time_t fMaxt=0 /* formatted max time */;

gotime = time (NULL);
system("date"); /* only for a good feedback on terminal */
time ( &fGotime );

fActual = fGotime;
fMint = fGotime;
fMaxt = fGotime;
fGotime += 0; /* !!!se aggiunta aggiorna i 3 valori sopra altrimenti rimangono settati a zero!!! why? */

for (;;){
for (i=0; i<10; i++){
actual = time (NULL);

ifp=fopen(CPUTEMP,"r");
if(ifp==NULL)
perror("");
fscanf(ifp, "%s %d", *&what, &a);
fclose(ifp);

if (a<b){ /* if a<b set the current temp as min */
mint = time (NULL);

b=a;
}
if (a>c){ /* if a>c set the current temp as max */
maxt = time (NULL);
c=a;
}

usleep(500000) ; /* sleep for .5 seconds */
}


//printf("%s\n\tnow: %d at %ldM %ldS\n\tmin: %d at %ldM %ldS\n\tmax: %d at %ldM %ldS\n", what, a, (actual-gotime)/60, (actual-gotime)%60, b, (mint-gotime)/60, (mint-gotime)%60, c, (maxt-gotime)/60, (maxt-gotime)%60);
//printf("a file with temperatures of cpu is created at /tmp/temp");
printf("fGotime: %s", ctime (&fGotime));
printf("%ld\n%ld\n%ld\n", actual-gotime, mint-gotime, maxt-gotime);
printf("fActu: %s fMint: %s fMaxt: %s\n", ctime (&fActual), ctime (&fMint), ctime (&fMaxt));

fMint += 10; //+ (int)(mint-gotime);
fMint += 0;
fActual += 60; //+ (int)(actual-gotime);
fActual += 0;
fMaxt += 30; //+ (int)(maxt-gotime);
fMaxt += 0;

printf("fActu: %s fMint: %s fMaxt: %s\n", ctime (&fActual), ctime (&fMint), ctime (&fMaxt));
ofp=fopen(TMP, "w+");
if(ofp==NULL)
perror("");
fprintf(ofp, "Started: %s\n%s\n\tnow: %d at %ldM %ldS\n\tmin: %d at %ldM %ldS\n\tmax: %d at %ldM %ldS\n", ctime (&fGotime), what, a, (actual-gotime)/60, (actual-gotime)%60, b, (mint-gotime)/60, (mint-gotime)%60, c, (maxt-gotime)/60, (maxt-gotime)%60);
fclose(ofp);
}

return 0;
}


il pezzo critico e' questo
fMint += 10; //+ (int)(mint-gotime);
fMint += 0;
fActual += 60; //+ (int)(actual-gotime);
fActual += 0;
fMaxt += 30; //+ (int)(maxt-gotime);
fMaxt += 0;
non riesco a capire xke' nn aggiorna questi tempi semplicemente aggiungendo degli interi, anzi sembra che aggiorni solo f actual

output
Fri Apr 3 16:10:46 CEST 2009
fGotime: Fri Apr 3 16:10:46 2009
5
0
0
fActu: Fri Apr 3 16:10:46 2009
fMint: Fri Apr 3 16:10:46 2009
fMaxt: Fri Apr 3 16:10:46 2009

fActu: Fri Apr 3 16:11:46 2009
fMint: Fri Apr 3 16:11:46 2009
fMaxt: Fri Apr 3 16:11:46 2009

fGotime: Fri Apr 3 16:10:46 2009
10
0
0
fActu: Fri Apr 3 16:11:46 2009
fMint: Fri Apr 3 16:11:46 2009
fMaxt: Fri Apr 3 16:11:46 2009

fActu: Fri Apr 3 16:12:46 2009
fMint: Fri Apr 3 16:12:46 2009
fMaxt: Fri Apr 3 16:12:46 2009 come si vede ogni passo aumenta tutti i valori d 60..

BrutPitt
03-04-2009, 22:30
Il problema e' ctime.

L'array che contiene il valore di ritorno di ctime e' una stringa che e' allocata staticamente e condivisa sia da ctime che da asctime.

Ogni volta che una di queste due funzioni e' chiamata, il contenuto dell'array e' sovrascritto.

Per cui una singola chiamata di printf con 3 chiamate a ctime stampa il medesimo contenuto dell'ultima chiamata a ctime.

Una cosa del genere, invece, stampa i contenuti che desideri:

printf("fMaxt: %s", ctime (&fMaxt));
printf("fActu: %s", ctime (&fActual));
printf("fMint: %s", ctime (&fMint));


In alternativa, prima di una nuova chiamata a ctime, devi farti la copia della stringa.

andrew1988
03-04-2009, 23:35
funziona perfettamente, grazie!