i need to save present date and time in text file when i input 1.and when i input 2 tha program needs to read that date and time from the file and compare it with present date and time and show the difference.i am trying for two days.but it is showing some wrong output.can some one solve the problem?thanx
Code:#include <stdio.h> #include <time.h> struct save { time_t t; }; int main(void) { int x; double sec; struct save a; struct tm * g,*h; time_t mytime; FILE *pfile; do { mytime=time(NULL); printf("1.Save 2.compare"); scanf("%d",&x); if(x==1) { a.t=time(NULL); pfile=fopen("time.txt","wb"); g = localtime (&a.t); printf ("Current local time and date: %s", asctime(g)); //showing current time and date fwrite(&a,sizeof(a),1,pfile); //saving current time and date fclose(pfile); } else { pfile=fopen("time.txt","wb"); fread(&a,sizeof(a),1,pfile); fclose(pfile); g = localtime (&a.t); printf ("Current local time and date: %s", asctime(g)); //showing the time from saved file h = localtime (&mytime); printf ("Current local time and date: %s", asctime(h)); //showing current time and date sec=difftime(mytime,a.t); //calculating difference printf("difference = %f",sec); } }while(x!=3); return 0; }