Today we will see how to get the System date and time as well as execute some DOS command through your ‘C’ program.
getdate() / setdate() - Gets or sets DOS system date
Declaration:
getdate(struct dosdate_t *datep);
setdate(struct dosdate_t *datep);
/* Example of getdate() / setdate() */
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void main()
{
struct date dt;
struct date nd;
clrscr();
getdate(&dt); //Getting the date from System
printf("\n\t\tSystem Date : %2d-%2d-%4d ",dt.da_day,dt.da_mon,dt.da_year);
nd.da_year = 2011;
nd.da_day = 10;
nd.da_mon = 7;
setdate(&nd); //Setting a New Date
printf("\n\t\tNew Date : %2d-%2d-%4d ",nd.da_day,nd.da_mon,nd.da_year);
getch();
}
Output :
System Date : 10- 7-2011
New Date : 10- 7-2011
gettime() / settime() - Get and set system time
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void main()
{
struct time tm;
struct time nt;
clrscr();
gettime(&tm); //Getting the Time from System
printf("\n\t\tSystem time : %2d:%2d:%2d ",tm.ti_hour,tm.ti_min,tm.ti_sec);
nt.ti_hour = 13;
nt.ti_min = 40;
nt.ti_sec = 15;
settime(&nt); //Setting a New Time
printf("\n\t\tNew Time : %2d:%2d:%2d ",nt.ti_hour,nt.ti_min,nt.ti_sec);
getch();
}
output:
System time : 13:40:16
New Time : 13:40:15
DOS – Use <dir.h> header file in addition to others for following DOS commands.
chdir() - Changes current directory. chdir causes the directory specified by path to become the current working, such as chdir("c:\TC\MY"). Given Directory path, must specify an existing directory.
getdcwd() - Gets the current working directory for specified drive.
getcurdir() - Gets current directory for specified drive.
mkdir() - creates a directory
rmdir() - Remove a existing directory present in current directory or given path.
perror() - Prints a System error message. perror prints to the stderr stream (normally the console) the system error message for the last library routine that produced the error.
/* Example of above DOS commansds */
#include <stdio.h>
#include <conio.h>
#include <dir.h>
void main()
{
char dir[12];
char ndir[12];
char buf[MAXPATH];
int chk;
clrscr();
getcwd(buf, MAXPATH);
printf("The current Working Directory directory is: %s\n", buf);
//Printing Current Directory Name
if (getcurdir(0, dir))
{
perror("getcurdir()");
exit(1);
}
printf("Current directory is: \\%s\n", dir);
//Creating a New Directory
printf("\nEnter A New Directory Name : ");
gets(ndir);
chk = mkdir(ndir);
if (!chk)
{
printf("Directory created\n");
}
else
{
printf("Unable to create directory\n");
getch();
exit(0);
}
if (chdir(ndir))
{
perror("chdir()");
exit(1);
}
if (getcurdir(0, ndir))
{
perror("getcurdir()");
exit(1);
}
printf("Current directory is now: \\%s\n", ndir);
printf("\nChanging back to original directory: \\%s\n", dir);
if (chdir("\\"))
{
perror("chdir()");
exit(1);
}
getcurdir(0, dir);
printf("Current directory is: \\%s\n", dir);
chk = rmdir(ndir);
if (!chk)
{
printf("\nDirectory deleted\n");
}
else
{
perror("\nUnable to delete directory\n");
}
getch();
}
Output:
The current Working Directory directory is: D:\
Current directory is: \
Enter A New Directory Name : TEMP
Directory created
Current directory is now: \TEMP
Changing back to original directory: \
Current directory is: \
Directory deleted
findfirst() / findnext() - findfirst and findfirst search a disk directory for files
/* findfirst and findnext example */
#include <stdio.h>
#include <conio.h>
#include <dir.h>
void main()
{
struct ffblk ffb;
int chk;
clrscr();
printf("Directory listing of *.*\n");
chk = findfirst("*.*",&ffb,0);
while (!chk)
{
printf(" %s\n", ffb.ff_name);
chk = findnext(&ffb);
}
getch();
}
Output
Directory listing of *.*
ACCHOLD.CPP
AD.CPP
ADD.CPP
ADJMAT.C
ADMTOL.C
ADMTOL.CPP
AINS.C
DT.C
NT.C
TC0000.SWP
TMP.C
There are thousands of built-in-functions available in ‘C’ and ‘C++’, it is not possible to cover all. I tried some useful or mostly used one. Next blog we will move to some other thing.