ArhEVM_Labs/main.c
Суслов Роман b8a9376514 v1
2023-09-28 14:20:48 +03:00

303 lines
9.5 KiB
C

#include <stdio.h>
#include <windows.h>
#include <locale.h>
int getLogicalDriveStrings() {
printf("- GetLogicalDriveStrings:\n");
char buffer[4096]; // ñàì áóôåð äëÿ ñòðîêè
DWORD bufSize = sizeof(buffer) - 1; // ðàçìåð áóôåðà, â êîòîðûé íóæíî çàïèñûâàòü ñòðîêó ñ äèñêàìè
GetLogicalDriveStrings(bufSize, buffer); // Ïîëó÷åíèå ñïèñêà ëîãè÷åñêèõ äèñêîâ
char *drive = buffer;
while (*drive) { // ïåðåáîð äèñêîâ
printf("Äèñê: %s\n", drive);
// ñëåäóþùèé äèñê
drive += strlen(drive) + 1;
}
return 0;
}
int getVolumeInformation() {
printf("- GetVolumeInformation:\n");
char volumeName[256];
char fileSystemName[256];
DWORD serialNumber;
DWORD maxComponentLength;
DWORD fileSystemFlags;
// Ïîëó÷åíèå èíôîðìàöèè î òåêóùåì äèñêå
GetVolumeInformation("C:\\", // óêàçàòåëü íà ñòðîêó, ñîäåðæàùóþ êîðíåâîé êàòàëîã òîìà
volumeName, // áóôåð, êîòîðûé ïîëó÷èò èìÿ òîìà
sizeof(volumeName), // äëèíà áóôåðà äëÿ èìåíè òîìà
&serialNumber, // óêàçàòåëü íà ïåðåìåííóþ, êîòîðàÿ ïîëó÷èò èìÿ óêàçàííîãî òîìà
&maxComponentLength, //
&fileSystemFlags, //
fileSystemName, // áóôåð äëÿ íàçâàíèÿ ôàéëîâîé ñèñòåìû
sizeof(fileSystemName)); // ðàçìåð áóôåðà íàçâàíèÿ ôàéëîâîé ñèñòåìû
printf("Èìÿ òîìà: %s\n", volumeName);
printf("Ñåðèéíûé íîìåð: %lu\n", serialNumber);
printf("Max Component Length: %lu\n", maxComponentLength);
printf("Òèï ôàéëîâîé ñèñòåìû: %s\n", fileSystemName);
printf("File System Flags: %lu\n", fileSystemFlags);
return 0;
}
// ---
long getFileSize() {
printf("- GetFileSize:\n");
HANDLE hFile;
DWORD fileSize;
unsigned long structLargeInt;
// Îòêðûòèå ôàéëà
hFile = CreateFile((LPCSTR) "../text.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Ïîëó÷åíèå ðàçìåðà ôàéëà
structLargeInt = GetFileSize(hFile, NULL);
// Çàêðûòèå ôàéëà
CloseHandle(hFile);
// Âûâîä ðàçìåðà ôàéëà
printf("Ðàçìåð ôàéëà: %lu áàéò\n", structLargeInt);
return 0;
}
int findNextFile() {
printf("- FindNextFile:\n");
WIN32_FIND_DATA findData;
HANDLE hFind;
char path[MAX_PATH];
// Çàäàåì ïóòü ê äèðåêòîðèè, êîòîðóþ íóæíî ïðîñêàíèðîâàòü
strcpy(path, "..\\*");
// Íà÷èíàåì ïîèñê ïåðâîãî ôàéëà/ïàïêè â äèðåêòîðèè
hFind = FindFirstFile(path, &findData);
// Ïåðåáèðàåì âñå ôàéëû è ïàïêè â äèðåêòîðèè
do {
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
printf("Ïàïêà: %s\n", findData.cFileName);
} else {
printf("\tÔàéë: %s\n", findData.cFileName);
}
} while (FindNextFile(hFind, &findData));
// Çàêðûòèå äåñêðèïòîðà ïîèñêà
FindClose(hFind);
return 0;
}
int getWindowsDirectory() {
printf("- GetWindowsDirectory:\n");
char windowsDir[255]; // ñþäà çàïèøóòñÿ ñèìâîëû ïóòè
GetWindowsDirectory(windowsDir, sizeof(windowsDir));
printf("Âèíäîñâ ïàïêà òóò: %s\n", windowsDir);
return 0;
}
int compareFileTime() {
printf("- CompareFileTime:\n");
FILETIME fileTime1;
FILETIME fileTime2;
// ïåðâûé ôàéë
HANDLE file1 = CreateFile((LPCSTR) "../text.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// âðåìÿ èçìåíåíèÿ ïåðâîãî ôàéëà
GetFileTime(file1, NULL, NULL, &fileTime1);
// çàêðûâàåì ïåðâûé ôàéë
CloseHandle(file1);
// Îòêðûâàåì âòîðîé ôàéë
HANDLE file2 = CreateFile((LPCSTR) "../text2.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Ïîëó÷àåì âðåìÿ èçìåíåíèÿ âòîðîãî ôàéëà
GetFileTime(file2, NULL, NULL, &fileTime2);
// Çàêðûâàåì âòîðîé ôàéë
CloseHandle(file2);
// Ñðàâíèâàåì âðåìÿ èçìåíåíèÿ ôàéëîâ
int comparisonResult = CompareFileTime(&fileTime1, &fileTime2);
if (comparisonResult == 1) {
printf("Ôàéë 1 áûë èçìåíåí ðàíüøå ôàéëà 2\n");
} else if (comparisonResult == -1) {
printf("Ôàéë 2 áûë èçìåíåí ðàíüøå ôàéëà 1\n");
} else {
printf("Âðåìÿ èçìåíåíèÿ ôàéëîâ îäèíàêîâî\n");
}
return 0;
}
// ---
int setLocalTime() {
printf("- SetLocalTime:\n");
// Ñîçäàíèå ñòðóêòóðû SYSTEMTIME äëÿ óñòàíîâêè âðåìåíè
SYSTEMTIME systemTime;
// Çàïîëíåíèå ñòðóêòóðû SYSTEMTIME äàííûìè òåêóùåãî âðåìåíè
GetLocalTime(&systemTime);
printf("\nÄàòà è âðåìÿ ÄÎ èçìåíåíèÿ: %02d-%02d-%d %02d:%02d:%02d\n",
systemTime.wDay, systemTime.wMonth, systemTime.wYear,
systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
// Óâåëè÷åíèå ÷àñîâîãî çíà÷åíèÿ íà 1
if (systemTime.wHour < 23) {
systemTime.wHour++;
} else {
systemTime.wHour = 0;
}
// Óñòàíîâêà íîâîãî âðåìåíè ñ èñïîëüçîâàíèåì SetLocalTime
SetLocalTime(&systemTime);
printf("Âðåìÿ óñïåøíî èçìåíåíî\n");
printf("\nÄàòà è âðåìÿ ÏÎÑËÅ èçìåíåíèÿ: %02d-%02d-%d %02d:%02d:%02d\n",
systemTime.wDay, systemTime.wMonth, systemTime.wYear,
systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
return 0;
}
int setFileAttributes() {
printf("- SetFileAttributes:\n");
// Góòü ê ôàéëó
LPCWSTR filePath = L"..\\text3.txt";
// Ñáðàñûâàåì àòðèáóòû ôàéëà
SetFileAttributes((LPCSTR) filePath, (DWORD) NULL);
// Òåêóùèå àòðèáóòû ôàéëà
DWORD fileAttributes = GetFileAttributes((LPCSTR) filePath);
printf("\tÒåêóùèå àòðèáóòû ôàéëà ÄÎ çàìåíû: ");
if (fileAttributes & FILE_ATTRIBUTE_READONLY) {
printf("\n- Read-only");
}
if (fileAttributes & FILE_ATTRIBUTE_HIDDEN) {
printf("\n- Hidden");
}
if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
printf("\n- Directory");
}// è òàê äàëåå äëÿ äðóãèõ àòðèáóòîâ ôàéëà...
printf("\n");
// Óñòàíàâëèâàåì àòðèáóòû ôàéëà
DWORD newFileAttributes = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY |fileAttributes;
BOOL result = SetFileAttributes((LPCSTR) filePath, (DWORD) newFileAttributes);
fileAttributes = GetFileAttributes((LPCSTR) filePath);
printf("\tÒåêóùèå àòðèáóòû ôàéëà ÏÎÑËÅ çàìåíû: ");
if (fileAttributes & FILE_ATTRIBUTE_READONLY) {
printf("\n- Read-only");
}
if (fileAttributes & FILE_ATTRIBUTE_HIDDEN) {
printf("\n- Hidden");
}
if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
printf("\n- Directory");
}// è òàê äàëåå äëÿ äðóãèõ àòðèáóòîâ ôàéëà...
printf("\n");
return 0;
}
int createFile() {
printf("- CreateFile:\n");
HANDLE hFile;
char* fileName = "example.txt";
// Ñîçäàíèå ôàéëà
hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// Çàïèñü â ôàéë
char* data = "Hello, World!";
DWORD bytesWritten;
WriteFile(hFile, data, strlen(data), &bytesWritten, NULL);
// Çàêðûòèå ôàéëà
CloseHandle(hFile);
// Ïîâòîðíîå îòêðûòèå ôàéëà
hFile = CreateFile(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// ×òåíèå èç ôàéëà
const int bufferSize = 100;
char buffer[bufferSize];
DWORD bytesRead;
if (!ReadFile(hFile, buffer, bufferSize, &bytesRead, NULL))
{
printf("Failed to read from file.\n");
CloseHandle(hFile);
return 1;
}
// Âûâîä ñîäåðæèìîãî ôàéëà
printf("Ñîäåðæèìîå ôàéëà: %.*s\n", bytesRead, buffer);
// Çàêðûòèå ôàéëà
CloseHandle(hFile);
return 0;
}
void copyFile(const char* sourceFile, const char* destinationFile) {
printf("- CopyFile:\n");
FILE* source = fopen(sourceFile, "rb");
FILE* destination = fopen(destinationFile, "wb");
// Êîïèðóåì ñîäåðæèìîå ôàéëà
char buffer[1024];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), source)) > 0) {
fwrite(buffer, 1, bytesRead, destination);
}
// Çàêðûâàåì ôàéëû
fclose(source);
fclose(destination);
printf("Ôàéë óñïåøíî ñêîïèðîâàí.\n");
}
int main() {
setlocale(LC_ALL, "Rus");
printf("\n----------------------------\n");
printf("\tÈíôîðìàöèÿ î äèñêàõ:\n");
// getLogicalDriveStrings();// + ïîëó÷åíèå áóêâ äèñêîâ
// getVolumeInformation();// + ïîëó÷åíèå èíôîðìàöèè î ôàéëîâîé ñèñòåìå âûáðàííîãî ëîãè÷åñêîãî äèñêà
printf("\n------------------------------------------\n");
printf("\tÈíôîðìàöèÿ î ôàéëàõ è äèðåêòîðèÿõ:\n");
// getFileSize();// + ïîëó÷àåò ðàçìåð ôàéëà
// findNextFile();// + ïîèñê ñëåäóþùåãî ôàéëà â ìåñòå ïîèñêà
// getWindowsDirectory();// + ïóòü ê îñíîâíîìó êàòàëîãó âèíäîâñ
// compareFileTime();// + îïðåäåëåíèå êàêîé èç ôàéëîâ áîëåå íîâûé
printf("\n----------------------------\n");
printf("\tÓïðàâëÿþùèå ôóíêöèè:\n");
// setLocalTime();// + óñòàíîâèòü òåêóùóþ äàòó è âðåìÿ
// setFileAttributes();// + óñòàíîâêà ôàéëîâûõ àòðèáóòîâ
// createFile();// ñîçäàòü/îòêðûòü ôàéë
const char* sourceFile = ".//text.txt";
const char* destinationFile = ".//text33.txt";
CopyFile(".//text.txt", ".//text3.txt", 0);// ñêîïèðîâàòü ôàéë
}