Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа №43 / 4 / lab4
.cpp// lab4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
void newfile();
void openexfile();
void infbyhandle();
void fileattr();
void systime();
void setfiletime();
void filetimetosys();
void comparefiletime();
void dostime();
char Filename1[_MAX_FNAME],Filename2[_MAX_FNAME];
LPBY_HANDLE_FILE_INFORMATION lpFileInformation = new _BY_HANDLE_FILE_INFORMATION;
HANDLE hFile1,hFile2,hFile3;
LPSYSTEMTIME lpSystemTime1 = new _SYSTEMTIME;
LPFILETIME lpFileTime1 = new _FILETIME;
LPFILETIME lpFileTime2 = new _FILETIME;
LPFILETIME lpCreationTime = new _FILETIME;
LPFILETIME lpCreationTime2 = new _FILETIME;
LPFILETIME lpCreationTime3 = new _FILETIME;
LPFILETIME lpLastAccessTime = new _FILETIME;
LPFILETIME lpLastWriteTime = new _FILETIME;
LPSYSTEMTIME lpSystemTime = new _SYSTEMTIME;
LPSYSTEMTIME ChangeCreationTime = new _SYSTEMTIME;
LPSYSTEMTIME ChangeLastAccessTime = new _SYSTEMTIME;
LPSYSTEMTIME ChangeLastWriteTime = new _SYSTEMTIME;
LPFILETIME lpNewCreationTime = new _FILETIME;
LPFILETIME lpNewLastAccessTime = new _FILETIME;
LPFILETIME lpNewLastWriteTime = new _FILETIME;
LPFILETIME lpFileTime = new _FILETIME;
LPSYSTEMTIME lpSystemTime2 = new _SYSTEMTIME;
LPSYSTEMTIME LocalLastAccessTime = new _SYSTEMTIME;
LPSYSTEMTIME LocalCreationTime = new _SYSTEMTIME;
LPSYSTEMTIME LocalLastWriteTime = new _SYSTEMTIME;
LPWORD lpFatDate = new WORD;
LPWORD lpFatTime = new WORD;
DWORD dwAttrs,Error;
LONG flag;
int a=0,b=0,c=0;
int _tmain(int argc, _TCHAR* argv[])
{
system("cls");
cout<<"\n The fourth laboratory work "<<endl;
cout<<" Operations with file characteristics"<<endl;
getch();
do
{
system("cls");
cout<<"\n Menu\n"<<endl;
cout<<" 1.Create file"<<endl;
cout<<" 2.Get file information by handle"<<endl;
cout<<" 3.Retrieving file attributes"<<endl;
cout<<" 4.Retrieving the current system date and time"<<endl;
cout<<" 5.Setting a file time"<<endl;
cout<<" 6.Converting a file time to system time format"<<endl;
cout<<" 7.Comparing two file times"<<endl;
cout<<" 8.Converting a file time to MS-DOS date and time values"<<endl;
cout<<" 9.Exit"<<endl;
do
{
cout<<"\n Vibirite punkt menu:"<<endl;
cin>>a;
if(a<0||a>9)
{
cout<<"\n Vi dopustili oshibku!!!(1-9)"<<endl;
getch();
}
}
while(a<0||a>9);
switch(a)
{
case 1:
{
system("cls");
cout<<"\n Menu\n"<<endl;
cout<<" 1.Create a new file"<<endl;
cout<<" 2.Open an existing file"<<endl;
cout<<"\n Vibirite punkt menu:"<<endl;
cin>>b;
do
{
if(b<0||b>2)
{
cout<<"\n Vi dopustili oshibku!!!(1-2)"<<endl;
getch();
}
}
while(b<0||b>2);
switch(b)
{
case 1:
{
newfile();break;
}
case 2:
{
openexfile();break;
}
}
break;
}
case 2:
{
infbyhandle();
break;
}
case 3:
{
fileattr();
break;
}
case 4:
{
systime();
break;
}
case 5:
{
setfiletime();
break;
}
case 6:
{
filetimetosys();
break;
}
case 7:
{
comparefiletime();
break;
}
case 8:
{
dostime();
break;
}
}
}
while(a!=9);
return 0;
}
void newfile(void)
{
system("cls");
cout<<"\nEnter the name of the file: \n"<<endl;
cin>>Filename1;
if(hFile1 = CreateFile(Filename1,GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0))
{
if (hFile1 == INVALID_HANDLE_VALUE)
{
cout<<"\nError!!!(maybe such file has already exists)"<<endl;
getch();
}
else
{
cout<<"File "<<Filename1<<" successfully created!"<<endl;
getch();
}
}
else
{
cout<<"\nError!!!"<<endl;
getch();
}
CloseHandle(hFile1);
}
void openexfile(void)
{
system("cls");
cout<<"\n\n\n\nEnter the name of the existing file :\n\n"<<endl;
cin>>Filename1;
if(hFile1 = CreateFile(Filename1,GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0))
{
if(hFile1 == INVALID_HANDLE_VALUE)
{
cout<<"\nError!!!(maybe such file does not exist)"<<endl;
getch();
}
else
{
cout<<"File "<<Filename1<<" successfully opened!"<<endl;
getch();
}
}
else
{
cout<<"\nError!!!"<<endl;
getch();
}
CloseHandle(hFile1);
}
void infbyhandle(void)
{
system("cls");
cout << "\nEnter the filename:" << endl;
cin >> Filename1;
if((hFile1 = CreateFile(Filename1,GENERIC_READ,0,0,OPEN_EXISTING,0,0)))
{
if(hFile1 == INVALID_HANDLE_VALUE)
{
cout<<"\nError!!!(maybe such file does not exist)"<<endl;
getch();
}
}
else
{
cout<<"\n\nError!!!"<<endl;
getch();
}
if(GetFileInformationByHandle(hFile1,lpFileInformation))
{
if(((lpFileInformation -> dwFileAttributes >> 5)& 1))
cout << "The file is an archive file" << endl;
if((lpFileInformation -> dwFileAttributes) & 1)
cout << "The file is read-only" << endl;
if(((lpFileInformation -> dwFileAttributes >> 1)& 1))
cout << "The file or is hidden" << endl;
if(((lpFileInformation -> dwFileAttributes >> 2)& 1))
cout << "The file is system" << endl;
lpCreationTime = &(lpFileInformation->ftCreationTime);
FileTimeToSystemTime(lpCreationTime, lpSystemTime);
cout << "\nCreation Time: ";
cout << "On " << lpSystemTime->wDay << "/" << lpSystemTime->wMonth << "/" << lpSystemTime->wYear;
cout << " at " <<lpSystemTime->wHour+3 << ":" << lpSystemTime->wMinute << ":" << lpSystemTime->wSecond << endl;
lpLastAccessTime = &(lpFileInformation->ftLastAccessTime);
FileTimeToSystemTime(lpLastAccessTime, lpSystemTime);
cout << "Last Access Time: ";
cout << "On " << lpSystemTime->wDay << "/" << lpSystemTime->wMonth << "/" << lpSystemTime->wYear;
cout << " at " <<lpSystemTime->wHour+3 << ":" << lpSystemTime->wMinute << ":" << lpSystemTime->wSecond << endl;
lpLastWriteTime = &(lpFileInformation->ftLastWriteTime);
FileTimeToSystemTime(lpLastWriteTime, lpSystemTime);
cout << "Last Write Time: ";
cout << "On " << lpSystemTime->wDay << "/" << lpSystemTime->wMonth << "/" << lpSystemTime->wYear;
cout << " at " <<lpSystemTime->wHour+3 << ":" << lpSystemTime->wMinute << ":" << lpSystemTime->wSecond << endl;
getch();
CloseHandle(hFile1);
}
}
void fileattr(void)
{
system("cls");
cout << "\nEnter the filename:" << endl;
cin >> Filename1;
if((dwAttrs=GetFileAttributes(Filename1))==INVALID_FILE_ATTRIBUTES)
{
cout<<"\nError!!!"<<endl;
getch();
}
else
{
if (dwAttrs & FILE_ATTRIBUTE_ARCHIVE)
cout<<"\nThe file is an archive file "<<endl;
if (dwAttrs & FILE_ATTRIBUTE_COMPRESSED)
cout<<"\nThe file is compressed "<<endl;
if (dwAttrs & FILE_ATTRIBUTE_ENCRYPTED)
cout<<"\nThe file is encrypted "<<endl;
if (dwAttrs & FILE_ATTRIBUTE_HIDDEN)
cout<<"\nThe file is hidden "<<endl;
if (dwAttrs & FILE_ATTRIBUTE_NORMAL)
cout<<"\nThe file has no other attributes set"<<endl;
if (dwAttrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
cout<<"\nThe file will not be indexed by the content indexing service"<<endl;
if (dwAttrs & FILE_ATTRIBUTE_READONLY)
cout<<"\nThe file is read-only "<<endl;
if (dwAttrs & FILE_ATTRIBUTE_SYSTEM)
cout<<"\nThe file is system "<<endl;
if (dwAttrs & FILE_ATTRIBUTE_TEMPORARY)
cout<<"\nThe file is being used for temporary storage"<<endl;
getch();
}
}
void systime(void)
{
system("cls");
GetSystemTime(lpSystemTime1);
cout<<"\n Year:"<<lpSystemTime1->wYear <<endl;
cout<<" Month:"<<lpSystemTime1->wMonth <<endl;
cout<<" Day of week:"<<lpSystemTime1->wDayOfWeek<<endl;
cout<<" Day:"<<lpSystemTime1->wDay<<endl;
cout<<" Hour:"<<lpSystemTime1->wHour+3<<endl;
cout<<" Minute:"<<lpSystemTime1->wMinute<<endl;
cout<<" Second:"<<lpSystemTime1->wSecond<<endl;
cout<<" Milliseconds:"<<lpSystemTime1->wMilliseconds<<endl;
getch();
}
void setfiletime(void)
{
system("cls");
cout << "\nEnter the filename:" << endl;
cin >> Filename1;
if(hFile1=CreateFile(Filename1,FILE_WRITE_ATTRIBUTES,0,0,OPEN_EXISTING,0,0))
{
cout << "File has been opened!" << endl;
getch();
}
else
{
cout<<"\nError!!!(maybe such file does not exist)"<<endl;
getch();
}
cout<<"\n1.Set creation time"<<endl;
cout<<"2.Set last access time"<<endl;
cout<<"3.Set last write time"<<endl;
cout<<"\n Vibirite punkt menu:"<<endl;
cin>>c;
do
{
if(c<0||c>3)
{
cout<<"\n Vi dopustili oshibku!!!(1-3)"<<endl;
getch();
}
}
while(c<0||c>3);
switch(c)
{
case 1:
{
ChangeCreationTime->wMilliseconds = 0;
ChangeCreationTime->wDayOfWeek = 0;
cout<<"Enter the day:"<<endl;
cin >> ChangeCreationTime->wDay;
cout << "Enter the month:" << endl;
cin >> ChangeCreationTime->wMonth;
cout << "Enter the Year:" << endl;
cin >> ChangeCreationTime->wYear;
cout << "Enter the Hour:" << endl;
cin>>ChangeCreationTime->wHour;
cout << "Enter the Minute:" << endl;
cin >> ChangeCreationTime->wMinute;
cout << "Enter the Second:" << endl;
cin >> ChangeCreationTime->wSecond;
TzSpecificLocalTimeToSystemTime(0,ChangeCreationTime,LocalCreationTime);
if(SystemTimeToFileTime(LocalCreationTime,lpNewCreationTime))
{
cout << "Converted" << endl;
getch();
}
else
{
cout << "Error converting" << endl;
Error = GetLastError();
cout << Error<< endl;
getch();
}
if(SetFileTime(hFile1,lpNewCreationTime,(LPFILETIME) NULL,(LPFILETIME) NULL))
{
cout << "\nTime has been set" << endl;
getch();
}
else
{
cout << "\nError setting time" << endl;
Error = GetLastError();
cout << Error;
getch();
}
break;
}
case 2:
{
ChangeLastAccessTime->wDayOfWeek = 0;
ChangeLastAccessTime->wMilliseconds = 0;
cout << "Enter the day:" << endl;
cin>> ChangeLastAccessTime->wDay;
cout << "Enter the month:" << endl;
cin >> ChangeLastAccessTime->wMonth;
cout << "Enter the Year:" << endl;
cin >> ChangeLastAccessTime->wYear;
cout << "Enter the Hour:" << endl;
cin >> ChangeLastAccessTime->wHour;
cout << "Enter the Minute:" << endl;
cin >> ChangeLastAccessTime->wMinute;
cout << "Enter the Second:" << endl;
cin >> ChangeLastAccessTime->wSecond;
TzSpecificLocalTimeToSystemTime(0,ChangeLastAccessTime,LocalLastAccessTime);
if(SystemTimeToFileTime(LocalLastAccessTime,lpNewLastAccessTime))
{
cout << "Converted" << endl;
getch();
}
else
{
cout << "Error converting" << endl;
Error = GetLastError();
cout << Error<< endl;
getch();
}
if(SetFileTime(hFile1,0,lpNewLastAccessTime,0))
{
cout << "\nTime has been set" << endl;
getch();
}
else
{
cout << "\nError setting time" << endl;
Error = GetLastError();
cout << Error;
getch();
}
break;
}
case 3:
{
ChangeLastWriteTime->wDayOfWeek = 0;
ChangeLastWriteTime->wMilliseconds = 0;
cout << "Enter the day:" << endl;
cin >> ChangeLastWriteTime->wDay;
cout << "Enter the month:" << endl;
cin >> ChangeLastWriteTime->wMonth;
cout << "Enter the Year:" << endl;
cin >> ChangeLastWriteTime->wYear;
cout << "Enter the Hour:" << endl;
cin >> ChangeLastWriteTime->wHour;
cout << "Enter the Minute:" << endl;
cin >> ChangeLastWriteTime->wMinute;
cout << "Enter the Second:" << endl;
cin >> ChangeLastWriteTime->wSecond;
TzSpecificLocalTimeToSystemTime(0,ChangeLastWriteTime,LocalLastWriteTime);
if(SystemTimeToFileTime(LocalLastWriteTime,lpNewLastWriteTime))
{
cout << "Converted" << endl;
getch();
}
else
{
cout << "Error converting" << endl;
Error = GetLastError();
cout << Error<< endl;
getch();
}
if(SetFileTime(hFile1, 0, 0, lpNewLastWriteTime))
{
cout << "\nTime has been set" << endl;
getch();
}
else
{
cout << "\nError setting time" << endl;
Error = GetLastError();
cout << Error;
getch();
}
break;
}
}
}
void filetimetosys(void)
{
system("cls");
cout << "\nEnter the filename:" << endl;
fflush(stdin);
cin >> Filename1;
if(hFile1=CreateFile(Filename1,GENERIC_READ,0,0,OPEN_EXISTING,0,0))
{
cout << "File has been opened!" << endl;
getch();
}
else
{
cout<<"\nError!!!(maybe such file does not exist)"<<endl;
getch();
}
if(GetFileTime(hFile1,lpCreationTime, 0, 0))
{
cout << "Getting the file time" << endl;
getch();
}
else
{
cout << "Error getting File Time" << endl;
getch();
}
if(FileTimeToSystemTime(lpCreationTime,lpSystemTime2))
{
cout<<"Converted to System time" << endl;
cout<<"\nYear:"<<lpSystemTime2->wYear <<endl;
cout<<"Month:"<<lpSystemTime2->wMonth <<endl;
cout<<"Day of week:"<<lpSystemTime2->wDayOfWeek<<endl;
cout<<"Day:"<<lpSystemTime2->wDay<<endl;
cout<<"Hour:"<<lpSystemTime2->wHour+3<<endl;
cout<<"Minute:"<<lpSystemTime2->wMinute<<endl;
cout<<"Second:"<<lpSystemTime2->wSecond<<endl;
cout<<"Milliseconds:"<<lpSystemTime2->wMilliseconds<<endl;
getch();
}
else
{
cout<<"Error!!! "<<endl;
Error = GetLastError();
cout<<Error;
getch();
}
CloseHandle(hFile1);
}
void comparefiletime(void)
{
system("cls");
cout << "\nEnter the first filename:" << endl;
cin >> Filename1;
cout << "Enter the second filename:" << endl;
cin >> Filename2;
if((hFile1=CreateFile(Filename1,GENERIC_READ,0,0,OPEN_EXISTING,0,0))&&(hFile2=CreateFile(Filename2,GENERIC_READ,0,0,OPEN_EXISTING,0,0)))
{
cout << "Files have been opened!" << endl;
getch();
}
else
{
cout<<"\nError!!!"<<endl;
getch();
}
if(GetFileTime(hFile1,lpFileTime1, 0, 0))
{
cout << "Getting the first file time" << endl;
getch();
}
else
{
cout << "Error getting File Time" << endl;
getch();
}
if(GetFileTime(hFile2,lpFileTime2, 0, 0))
{
cout << "Getting the second file time" << endl;
getch();
}
else
{
cout << "Error getting File Time" << endl;
getch();
}
flag=CompareFileTime(lpFileTime1,lpFileTime2);
switch(flag)
{
case -1:
{
cout<<"First file time is earlier than second file time"<<endl;getch();break;
}
case 0:
{
cout<<"First file time is equal to second file time"<<endl;getch();break;
}
case 1:
{
cout<<"First file time is later than second file time"<<endl;getch();break;
}
}
}
void dostime(void)
{
system("cls");
cout << "Enter the filename:" << endl;
cin >> Filename1;
if(hFile3 = CreateFile(Filename1, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0))
{
cout << "File opened!" << endl;
getch();
}
else
{
cout << "Error!" << endl;
getch();
}
if(GetFileTime(hFile3,lpCreationTime3,0,0))
{
cout << "Getting the file time" << endl;
getch();
}
else
{
cout << "Error getting File Time" << endl;
Error = GetLastError();
cout<<Error;
getch();
}
if(FileTimeToDosDateTime(lpCreationTime3,lpFatDate,lpFatTime))
{
cout << "Converted to DOS date" << endl;
unsigned short Day = (*lpFatDate)&31;
unsigned short Month = ((*lpFatDate)>>5)&15;
unsigned short Year = ((*lpFatDate)>>9)&127;
cout << "Converted to DOS time: ";
cout << "On " << Day << "/" << Month << "/" << Year+1980;
unsigned short Second = (*lpFatTime)&31;
unsigned short Minute = ((*lpFatTime)>>5)&63;
unsigned short Hour = ((*lpFatTime)>>11)&31;
cout << " at " <<Hour+3 << ":" << Minute << ":" << Second*2 << endl;
getch();
}
}