Tuesday, January 13, 2015

Reading system value through system function

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>

int main()
{
        //Variable declaration
        FILE *fp;
        char hc1,hc2,mc1,mc2;
        int hi1,hi2,mi1,mi2,hour,minute;
        //Reading system value through system function 
        system("echo %time% >time.txt");
        fp=fopen("time.txt","r");
        // Sanity Check
        if(fp==NULL)
           exit(1) ;
        //Assigning individual character information into char varialbels
        hc1=fgetc(fp);
        hc2=fgetc(fp);
        fgetc(fp); // to avoid : symbol from integer calculation
        mc1=fgetc(fp);
        mc2=fgetc(fp);
        fclose(fp);
        remove("time.txt");
       
        hi1=hc1;
        hi2=hc2;
        mi1=mc1;
        mi2=mc2;
        // Char to Integer conversion
        hi1-=48;
        hi2-=48;
        mi1-=48;
        mi2-=48;
        // adding characters for double digit
        hour=hi1*10+hi2;
        minute=mi1*10+mi2;
        //Final output
        printf("Current time is %d:%d\n",hour,minute);


        return 0;
}

Output:
Current time is 11:40

No comments:

Post a Comment