Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Saturday, 6 July 2013

Data Files - C Interview Questions IV


Q. How do you list a file's date and time?
A file's date and time are stored in the find_t structure returned from the _dos_findfirst() and_dos_findnext() functions.
The date and time stamp of the file is stored in the find_t.wr_date and find_t.wr_time structure members. The file date is stored in a two-byte unsigned integer as shown here:
Element

Offset

Range
Seconds
-
5 bits
-
0-9 (multiply by 2 to get the seconds value)
Minutes
-
6 bits
-
0-59
Hours
-
5 bits
-
0-23
Similarly, the file time is stored in a two-byte unsigned integer, as shown here:
Element

Offset

Range
Day
-
5 bits
-
1-31
Month
-
4 bits
-
1-12
Year
-
7 bits
-
0-127 (add the value "1980" to get the year value)
Because DOS stores a file's seconds in two-second intervals, only the values 0 to 29 are needed. You simply multiply the value by 2 to get the file's true seconds value. Also, because DOS came into existence in 1980, no files can have a time stamp prior to that year. Therefore, you must add the value "1980" to get the file's true year value.
The following example program shows how you can get a directory listing along with each file's date and time stamp:
#include <stdio.h>
#include <direct.h>
#include <dos.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
     FILE_BLOCK f_block;   /* Define the find_t structure variable */
     int ret_code;         /* Define a variable to store return codes */
     int hour;             /* We're going to use a 12-hour clock! */
     char* am_pm;          /* Used to print "am" or "pm" */
     printf("\nDirectory listing of all files in this directory:\n\n");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list
        all files in the directory, including system files, hidden
        files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block);
     /* The _dos_findfirst() function returns a 0 when it is successful
        and has found a valid filename in the directory. */
     while (ret_code == 0)
     {
          /* Convert from a 24-hour format to a 12-hour format. */
          hour = (f_block.wr_time >> 11);
          if (hour > 12)
          {
               hour  = hour - 12;
               am_pm = "pm";
          }
          else
               am_pm = "am";
          /* Print the file's name, date stamp, and time stamp. */
          printf("%-12s  %02d/%02d/%4d  %02d:%02d:%02d %s\n",
                    f_block.name,                      /* name  */
                    (f_block.wr_date >> 5) & 0x0F,     /* month */
                    (f_block.wr_date) & 0x1F,          /* day   */
                    (f_block.wr_date >> 9) + 1980,     /* year  */
                    hour,                              /* hour  */
                    (f_block.wr_time >> 5) & 0x3F,     /* minute  */
                    (f_block.wr_time & 0x1F) * 2,      /* seconds */
                    am_pm);
          /* Use the _dos_findnext() function to look
             for the next file in the directory. */
          ret_code = _dos_findnext(&f_block);
     }
     printf("\nEnd of directory listing.\n");
}
Notice that a lot of bit-shifting and bit-manipulating had to be done to get the elements of the time variable and the elements of the date variable. If you happen to suffer from bitshiftophobia (fear of shifting bits), you can optionally code the preceding example by forming a union between the find_t structure and your own user-defined structure, such as this:
/* This is the find_t structure as defined by ANSI C. */
struct find_t
{
     char reserved[21];
     char attrib;
     unsigned wr_time;
     unsigned wr_date;
     long size;
     char name[13];
}
/* This is a custom find_t structure where we
   separate out the bits used for date and time. */
struct my_find_t
{
     char reserved[21];
     char attrib;
     unsigned seconds:5;
     unsigned minutes:6;
     unsigned hours:5;
     unsigned day:5;
     unsigned month:4;
     unsigned year:7;
     long size;
     char name[13];
}
/* Now, create a union between these two structures
   so that we can more easily access the elements of
   wr_date and wr_time. */
union file_info
{
     struct find_t ft;
     struct my_find_t mft;
}
Using the preceding technique, instead of using bit-shifting and bit-manipulating, you can now extract date and time elements like this:
...
file_info my_file;
...
printf("%-12s  %02d/%02d/%4d  %02d:%02d:%02d %s\n",
          my_file.mft.name,             /* name    */
          my_file.mft.month,            /* month   */
          my_file.mft.day,              /* day     */
          (my_file.mft.year + 1980),    /* year    */
          my_file.mft.hours,            /* hour    */
          my_file.mft.minutes,          /* minute  */
          (my_file.mft.seconds * 2),    /* seconds */
          am_pm);

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...