264 lines
6.8 KiB
C++
264 lines
6.8 KiB
C++
#include <stdio.h>
|
|
#include <windows.h>
|
|
#include <stdio.h>
|
|
#include <conio.h>
|
|
//сделать применение функций
|
|
//GetLogicalDriverStrings V
|
|
//GetDriveType V
|
|
//GetCurrentDirectoryV
|
|
//GetFileAttributesV
|
|
//GetSystemDirectoryV
|
|
//GetTempPathV
|
|
//SetCurrentDirectoryV
|
|
//SetFileAttributesV
|
|
//CreateFileV
|
|
//ReadFileV
|
|
//WriteFileV
|
|
|
|
// for path use this: D:\c\lab1
|
|
// for attributeChekc use: D:\c\lab1\text.txt
|
|
int getAllDisk(){
|
|
DWORD dwSize = MAX_PATH;
|
|
char szLogicalDrives[MAX_PATH] = {0};
|
|
WORD dwResult = GetLogicalDriveStrings(dwSize,szLogicalDrives);
|
|
if (dwResult > 0 && dwResult <= MAX_PATH)
|
|
{
|
|
char* szSingleDrive = szLogicalDrives;
|
|
while(*szSingleDrive)
|
|
{
|
|
printf("Drive: %s\n", szSingleDrive);
|
|
|
|
// get the next drive
|
|
szSingleDrive += strlen(szSingleDrive) + 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
int getDriveType(){
|
|
DWORD dwSize = MAX_PATH;
|
|
char szLogicalDrives[MAX_PATH] = {0};
|
|
WORD dwResult = GetLogicalDriveStrings(dwSize,szLogicalDrives);
|
|
if (dwResult > 0 && dwResult <= MAX_PATH)
|
|
{
|
|
char* szSingleDrive = szLogicalDrives;
|
|
while(*szSingleDrive)
|
|
{
|
|
printf("Drive: %s", szSingleDrive);
|
|
switch(GetDriveType(szSingleDrive)){
|
|
case 0:
|
|
printf("Error.\n");
|
|
break;
|
|
case 1:
|
|
printf("Drive does not exist.\n");
|
|
break;
|
|
case 2:
|
|
printf("Removable media.\n");
|
|
break;
|
|
case 3:
|
|
printf("Fixed disk.\n");
|
|
break;
|
|
case 4:
|
|
printf("Network drive.\n");
|
|
break;
|
|
case 5:
|
|
printf("CD-ROM drive.\n");
|
|
break;
|
|
}
|
|
// get the next drive
|
|
szSingleDrive += strlen(szSingleDrive) + 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
int getCurrentDirectory(){
|
|
DWORD dwSize = MAX_PATH;
|
|
char szLogicalDrives[MAX_PATH] = {0};
|
|
WORD dwResult = GetCurrentDirectory(dwSize,szLogicalDrives);
|
|
if (dwResult > 0 && dwResult <= MAX_PATH)
|
|
{
|
|
char* szSingleDrive = szLogicalDrives;
|
|
|
|
printf("CurrentDirectory: %s\n", szSingleDrive);
|
|
|
|
// get the next drive
|
|
//szSingleDrive += strlen(szSingleDrive) + 1;
|
|
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
int getFileAttribute(char path[]){
|
|
|
|
switch(GetFileAttributes(path)){
|
|
case FILE_ATTRIBUTE_ARCHIVE:
|
|
printf("Archived.\n");
|
|
break;
|
|
case FILE_ATTRIBUTE_COMPRESSED:
|
|
printf("Compressed\n");
|
|
break;
|
|
case FILE_ATTRIBUTE_DIRECTORY:
|
|
printf("FolderName\n");
|
|
break;
|
|
case FILE_ATTRIBUTE_HIDDEN :
|
|
printf("hidden\n");
|
|
break;
|
|
case FILE_ATTRIBUTE_NORMAL:
|
|
printf("Normal \n");
|
|
break;
|
|
case FILE_ATTRIBUTE_READONLY :
|
|
printf("readOnly\n");
|
|
break;
|
|
case FILE_ATTRIBUTE_SYSTEM :
|
|
printf("systemFile\n");
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|
|
int getSystemDirectory(){
|
|
DWORD dwSize = MAX_PATH;
|
|
char szLogicalDrives[MAX_PATH] = {0};
|
|
WORD dwResult = GetSystemDirectory(szLogicalDrives,dwSize);
|
|
if (dwResult > 0 && dwResult <= MAX_PATH)
|
|
{
|
|
char* szSingleDrive = szLogicalDrives;
|
|
|
|
printf("SystemDirectory: %s\n", szSingleDrive);
|
|
|
|
|
|
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
int getTempPath(){
|
|
DWORD dwSize = MAX_PATH;
|
|
char szLogicalDrives[MAX_PATH] = {0};
|
|
WORD dwResult = GetTempPath(dwSize,szLogicalDrives);
|
|
if (dwResult > 0 && dwResult <= MAX_PATH)
|
|
{
|
|
char* szSingleDrive = szLogicalDrives;
|
|
|
|
printf("TempPath: %s\n", szSingleDrive);
|
|
|
|
|
|
|
|
}
|
|
return 0;
|
|
}
|
|
int setCurrentDirectory(char path[]){
|
|
SetCurrentDirectory(path);
|
|
getCurrentDirectory();
|
|
return 0;
|
|
}
|
|
int setFileAttribute(char path[],int attribute){
|
|
switch(attribute){
|
|
case 1:
|
|
SetFileAttributes(path,FILE_ATTRIBUTE_ARCHIVE);
|
|
break;
|
|
case 2:
|
|
SetFileAttributes(path,FILE_ATTRIBUTE_COMPRESSED);
|
|
break;
|
|
case 3:
|
|
SetFileAttributes(path,FILE_ATTRIBUTE_DIRECTORY);
|
|
break;
|
|
case 4 :
|
|
SetFileAttributes(path,FILE_ATTRIBUTE_HIDDEN);
|
|
break;
|
|
case 5:
|
|
SetFileAttributes(path,FILE_ATTRIBUTE_NORMAL);
|
|
break;
|
|
case 6 :
|
|
SetFileAttributes(path,FILE_ATTRIBUTE_READONLY);
|
|
break;
|
|
case 7 :
|
|
SetFileAttributes(path,FILE_ATTRIBUTE_SYSTEM);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
getFileAttribute(path);
|
|
return 0;
|
|
}
|
|
int writeFile(char path[],char message[]){
|
|
DWORD bytesWritten;
|
|
HANDLE hFile = CreateFile(path, FILE_WRITE_ACCESS, FILE_SHARE_READ,
|
|
NULL, OPEN_ALWAYS, 0, NULL);
|
|
WriteFile(hFile, message, strlen(message), &bytesWritten, NULL);
|
|
return 0;
|
|
}
|
|
int readFile(char path[]){
|
|
char readBuffer[256];
|
|
DWORD bytesRead;
|
|
|
|
HANDLE fh = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if(fh!=INVALID_HANDLE_VALUE){
|
|
printf("File opened for reading!\n");
|
|
} else {
|
|
printf("File not opened for reading!\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if(ReadFile(fh, readBuffer, sizeof(readBuffer), &bytesRead, NULL)){
|
|
printf("File successfully read!\n");
|
|
printf("%lu bytes read.\n", bytesRead);
|
|
printf("%s\n", readBuffer);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
int main() {
|
|
printf("GetAllDisk:\n");
|
|
getAllDisk();
|
|
|
|
printf("GetDriveType:\n");
|
|
getDriveType();
|
|
|
|
printf("GetCurrentDirectory:\n");
|
|
getCurrentDirectory();
|
|
|
|
printf("GetFileAttribute:\n");
|
|
char userInput[MAX_PATH];
|
|
printf("Input path\n");
|
|
// scanf("%260s",userInput);
|
|
//getFileAttribute( userInput);
|
|
|
|
printf("GetSystemDirectory:\n");
|
|
getSystemDirectory();
|
|
|
|
printf("GetTempPath:\n");
|
|
getTempPath();
|
|
|
|
printf("SetCurrentDirectory:\n");
|
|
|
|
printf("Input path\n");
|
|
//scanf("%260s",userInput);
|
|
//setCurrentDirectory(userInput);
|
|
printf("SetFileAttribute\n");
|
|
printf("Input path\n");
|
|
//scanf("%260s",userInput);
|
|
int userChoice;
|
|
printf("choise attribute:\n 1-archive\n2-compressed\n3-directory\n4-hidden\n5-normal\n6-readonly\n7-system\n");
|
|
//scanf("%d",&userChoice);
|
|
//setFileAttribute(userInput,userChoice);
|
|
|
|
printf("CreateFile:\n");
|
|
printf("Input path\n");
|
|
//scanf("%260s",userInput);
|
|
// createFile(userInput);
|
|
|
|
printf("WriteFile:\n");
|
|
printf("Input path\n");
|
|
//scanf("%260s",userInput);
|
|
char userMessage[1024];
|
|
printf("Input text that you want write to file\n");
|
|
//scanf("%1024s",userMessage);
|
|
//writeFile(userInput,userMessage);
|
|
|
|
printf("ReadFile:\n");
|
|
scanf("%260s",userInput);
|
|
readFile(userInput);
|
|
|
|
return 0;
|
|
}
|