Thread: [NEED HELP]need help to solve a problem in calculating difference between time

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    1

    [NEED HELP]need help to solve a problem in calculating difference between time

    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;
        }


  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by tanvir108115 View Post
    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:
    […]
            else
            {
                pfile=fopen("time.txt","wb");
                fread(&a,sizeof(a),1,pfile);
                fclose(pfile);
    […]
    You open the file for 'wb' (write binary). That means that the content of the file will truncated and the read/write-pointer stay at position 0.
    And you should check if the file is successfull opened.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating time
    By mushy in forum C Programming
    Replies: 9
    Last Post: 02-01-2008, 05:44 AM
  2. Time Calculating
    By kumar14878 in forum C Programming
    Replies: 2
    Last Post: 04-23-2005, 06:08 AM
  3. Replies: 3
    Last Post: 11-15-2003, 11:20 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM

Tags for this Thread