Last active
October 11, 2019 21:11
-
-
Save archi144/bb29616d1a0905540e34256bad8ffa07 to your computer and use it in GitHub Desktop.
Работа над/с ошибками
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
using namespace std; | |
int main() | |
{ | |
open("./1.cpp",O_RDONLY); // открываем файл с правами только на чтение | |
cout<<"errno - " <<errno<<endl; // возвращает код последней операции | |
cout<<"sys_errlist[] - "<<sys_errlist[errno]<<endl; // массив возвращаемых значений | |
perror(sys_errlist[errno]); // вместо номера ошибки выводит описание ошибки | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
using namespace std; | |
int main(int argc, char* argv[]) | |
{ | |
int file; // дескриптор файла | |
if (argv[1] == nullptr) | |
{ | |
char name[10]{}; | |
cout << "Введите имя файла: "; | |
cin >> name; | |
file = open(name, O_RDONLY); | |
int end = lseek(file, 0, SEEK_END); | |
while (end--) | |
{ | |
lseek(file, end, SEEK_SET); | |
char buff[1]; | |
read(file, buff, 1); | |
cout << buff[0]; | |
} | |
} | |
else | |
{ | |
file = open(argv[1], O_RDONLY); | |
int end = lseek(file, 0, SEEK_END); | |
while (end--) | |
{ | |
lseek(file, end, SEEK_SET); | |
char buff[1]; | |
read(file, buff, 1); | |
cout << buff[0]; | |
} | |
} | |
close(file); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
using namespace std; | |
int main(int argc, char* argv[]) | |
{ | |
struct stat fileInfo[argc]; | |
int greatest; | |
for (int i = 0; i < argc; i++) // заполняем массив структур статистикой о файлах, передаваемыех в параметрах | |
{ | |
stat(argv[i], &fileInfo[i]); | |
} | |
for (int i = 0; i < argc - 1 ; i++) // сравнием размеры полученных файлов | |
{ | |
int firstVal = fileInfo[i].st_size; | |
int secondVal = fileInfo[i + 1].st_size; | |
firstVal > secondVal ? greatest = firstVal : greatest = secondVal; | |
} | |
cout << "Наибольший файл размером: " << greatest << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
#define SIZE 20 | |
using namespace std; | |
int main() | |
{ | |
char name[20]; // имя файла | |
mode_t rights; // права | |
int change; | |
cout << "Введите имя файла: "; | |
cin >> name; | |
cout << "\nВыберите права доступа файла: " << endl; | |
cout << "1. Право на чтение \t 2. Право на запись " | |
<< "\n3. Право на выполнение \t 4. Всё вместе." << endl; | |
scanf("%d", &change); | |
switch (change) | |
{ | |
case 1: | |
rights = READ; | |
break; | |
case 2: | |
rights = WRITE; | |
break; | |
case 3: | |
rights = EXEC; | |
break; | |
case 4: | |
rights = ALL; | |
break; | |
default: | |
cout << "ERROR" << endl; | |
} | |
int file = creat(name, rights); | |
cout << "Дескриптор созданного файла " << name << ": " << file << endl; | |
char str[] {"1.One\n2.Two\n3.Three\n4.Four\n5.Five"}; | |
cout << "Записываем в файл " << name << "..." << endl; | |
int lengthSTR = strlen(str); // длина записанной строки | |
write(file, str, lengthSTR); // сохраняем длину строки, которую записали в файл | |
close(file); | |
cout << "Закрыли файл " << name << "..." << endl; | |
cout << "Открываем файл " << name << " на чтение..." << endl; | |
file = open(name, O_RDONLY); // открываем файл на чтение | |
cout << "Дескриптор открытого файла " << name << ": " << file << endl; | |
char buffer[lengthSTR - 1]; // все проблемы исходят отсюда | |
read(file, buffer, lengthSTR); | |
cout << "Содержимое файла " << name << ": " << endl; | |
cout << buffer << endl; | |
close(file); | |
cout << "Закрыли файл " << name << "..." << endl; | |
cout << "Открываем файл " << name << " на чтение и запись" << endl; | |
cout << "Дескриптор вновь открытого файла " << name << ": " << open(name, O_RDWR) << endl; // O_RDWR - чтение и запись | |
cout << strerror(errno) << endl; | |
close(file); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
#define SIZE 20 | |
using namespace std; | |
int main() | |
{ | |
char name[20]; // имя файла | |
mode_t rights; // права | |
int change; | |
cout << "Введите имя файла: "; | |
cin >> name; | |
cout << "\nВыберите права доступа файла: " << endl; | |
cout << "1. Право на чтение \t 2. Право на запись " | |
<< "\n3. Право на выполнение \t 4. Всё вместе." << endl; | |
scanf("%d", &change); | |
switch (change) | |
{ | |
case 1: | |
rights = READ; | |
break; | |
case 2: | |
rights = WRITE; | |
break; | |
case 3: | |
rights = EXEC; | |
break; | |
case 4: | |
rights = ALL; | |
break; | |
default: | |
cout << "ERROR" << endl; | |
} | |
int file = creat(name, rights); | |
cout << "Дескриптор созданного файла " << name << ": " << file << endl; | |
char str[] {"1.One\n2.Two\n3.Three\n4.Four\n5.Five"}; | |
cout << "Записываем в файл " << name << "..." << endl; | |
int lengthSTR = strlen(str); | |
int writeLen = write(file, str, lengthSTR); // сохраняем длину строки, которую записали в файл | |
cout << "Длина записанной строки - " << writeLen << endl; | |
cout << "Возвращаемое значение close(" << name << ") - " << close(file) << endl; | |
cout << "Закрыли файл " << name << "..." << endl; | |
cout << "Открываем файл " << name << " на чтение..." << endl; | |
file = open(name, O_RDONLY); // открываем файл на чтение | |
cout << "Дескриптор открытого файла " << name << ": " << file << endl; | |
char buffer[lengthSTR - 1]; // все проблемы исходят отсюда | |
int readLen = read(file, buffer, lengthSTR); // количество прочитанных символов | |
cout << "Количество прочитанных символов - " << readLen << endl; | |
cout << "Содержимое файла " << name << ": " << endl; | |
cout << buffer << endl; | |
cout << "Возвращаемое значение close(" << name << ") - " << close(file) << endl; | |
cout << "Закрыли файл " << name << "..." << endl; | |
cout << "Открываем файл " << name << " на чтение и запись" << endl; | |
cout << "Дескриптор вновь открытого файла " << name << ": " << open(name, O_RDWR) << endl; // O_RDWR - чтение и запись | |
cout << strerror(errno) << endl; | |
close(file); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
#define SIZE 20 | |
using namespace std; | |
int main() | |
{ | |
char name[20]; // имя файла | |
mode_t rights; // права | |
int change; | |
cout << "Введите имя файла: "; | |
cin >> name; | |
cout << "\nВыберите права доступа файла: " << endl; | |
cout << "1. Право на чтение \t 2. Право на запись " | |
<< "\n3. Право на выполнение \t 4. Всё вместе." << endl; | |
scanf("%d", &change); | |
switch (change) | |
{ | |
case 1: | |
rights = READ; | |
break; | |
case 2: | |
rights = WRITE; | |
break; | |
case 3: | |
rights = EXEC; | |
break; | |
case 4: | |
rights = ALL; | |
break; | |
default: | |
cout << "ERROR" << endl; | |
} | |
int file = creat(name, rights); | |
char str1[] {"11"}; | |
int lengthSTR = strlen(str1); | |
int offset; | |
cout << "Задайте смещение записи в файл относительно начала: "; | |
cin >> offset; | |
cout << endl; | |
write(file, "0000000000", 10); | |
write(file, "0000000000", 10); | |
write(file, "0000000000", 10); | |
write(file, "0000000000", 10); | |
write(file, "0000000000", 10); | |
lseek(file, offset, SEEK_SET); | |
cout << "Записываем в " << name << " со смещением " << offset << " относительно начала файла..." << endl; | |
write(file, str1, lengthSTR); | |
cout << "\nЗадайте смещение записи в файл относительно текущей позиции: "; | |
cin >> offset; | |
cout << endl; | |
lseek(file, offset, SEEK_CUR); | |
char str2[] {"2222"}; | |
lengthSTR = strlen(str2); | |
cout << "Записываем в " << name << " со смещением " << offset << " относительно текущей позиции в файле..." << endl; | |
write(file, str2, lengthSTR); | |
cout << "\nЗадайте смещение записи со знаком минус в файл относительно конца файла: "; | |
cin >> offset; | |
cout << endl; | |
lseek(file, offset, SEEK_END); | |
char str3[] {"333333"}; | |
lengthSTR = strlen(str3); | |
cout << "Записываем в " << name << " со смещением " << offset << " относительно конца файла..." << endl; | |
write(file, str3, lengthSTR); | |
//fwrite(str3, sizeof(char), lengthSTR, file); | |
close(file); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
#define SIZE 20 | |
using namespace std; | |
int main() | |
{ | |
char name[20]; // имя файла | |
mode_t rights; // права | |
int change; | |
cout << "Введите имя файла: "; | |
cin >> name; | |
cout << "\nВыберите права доступа файла: " << endl; | |
cout << "1. Право на чтение \t 2. Право на запись " | |
<< "\n3. Право на выполнение \t 4. Всё вместе." << endl; | |
scanf("%d", &change); | |
switch (change) | |
{ | |
case 1: | |
rights = READ; | |
break; | |
case 2: | |
rights = WRITE; | |
break; | |
case 3: | |
rights = EXEC; | |
break; | |
case 4: | |
rights = ALL; | |
break; | |
default: | |
cout << "ERROR" << endl; | |
} | |
int file = creat(name, rights); | |
cout << "Дескриптор созданного файла - " << file << endl; | |
char str1[] {"11"}; | |
int lengthSTR = strlen(str1); | |
cout << "Количество записанных символов в файл - " << write(file, "0000000000", 10) << endl; | |
cout << "Количество записанных символов в файл - " << write(file, "0000000000", 10) << endl; | |
cout << "Количество записанных символов в файл - " << write(file, "0000000000", 10) << endl; | |
cout << "Количество записанных символов в файл - " << write(file, "0000000000", 10) << endl; | |
cout << "Количество записанных символов в файл - " << write(file, "0000000000", 10) << endl; | |
int offset; | |
cout << "Задайте смещение записи в файл относительно начала: "; | |
cin >> offset; | |
cout << endl; | |
cout << "\nСмещение в байтах - " << lseek(file, offset, SEEK_SET) << endl; | |
cout << "Записываем в " << name << " со смещением " << offset << " относительно начала файла..." << endl; | |
cout << "Количество записанных символов в файл - " << write(file, str1, lengthSTR) << endl; | |
cout << "\nЗадайте смещение записи в файл относительно текущей позиции: "; | |
cin >> offset; | |
cout << endl; | |
cout << "\nСмещение в байтах - " << lseek(file, offset, SEEK_CUR) << endl; | |
char str2[] {"2222"}; | |
lengthSTR = strlen(str2); | |
cout << "Записываем в " << name << " со смещением " << offset << " относительно текущей позиции в файле..." << endl; | |
cout << "Количество записанных символов в файл - " << write(file, str2, lengthSTR) << endl; | |
cout << "\nЗадайте смещение записи со знаком минус в файл относительно конца файла: "; | |
cin >> offset; | |
cout << endl; | |
cout << "\nСмещение в байтах - " << lseek(file, offset, SEEK_END) << endl; | |
char str3[] {"333333"}; | |
lengthSTR = strlen(str3); | |
cout << "Записываем в " << name << " со смещением " << offset << " относительно конца файла..." << endl; | |
cout << "Количество записанных символов в файл - " << write(file, str3, lengthSTR) << endl; | |
//fwrite(str3, sizeof(char), lengthSTR, file); | |
close(file); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
using namespace std; | |
int main() | |
{ | |
int file = open("ALL", O_RDWR | O_CREAT, S_IRWXU); // открыть файл с правами чтения и записи, если файла нет - создать | |
char str[] {"1234"}; | |
write(file, str, strlen(str)); | |
lseek(file, 100000, SEEK_CUR); // смещение на 100 000 байт от текущей позиции | |
write(file, str, strlen(str)); | |
close(file); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
using namespace std; | |
int main(int argc, char* argv[]) | |
{ | |
setlocale(LC_ALL, ""); | |
struct stat fileInfo; // структура хранящая информацию об файле | |
stat(argv[1], &fileInfo); | |
char buff[100]; | |
cout << "Информация о файле " << argv[1] << ":" << endl; | |
cout << "Устройство: " << fileInfo.st_dev << endl; | |
cout << "inode: " << fileInfo.st_ino << endl; | |
cout << "Режим доступа: " << fileInfo.st_mode << endl; | |
cout << "Кол-во жестких ссылок: " << fileInfo.st_nlink << endl; | |
cout << "Идентификатор пользователя-владельца: " << fileInfo.st_uid << endl; | |
cout << "Идентификатор группы-владельца: " << fileInfo.st_gid << endl; | |
cout << "Тип устройства: " << fileInfo.st_rdev << endl; | |
cout << "Общий размер в байтах: " << fileInfo.st_size << endl; | |
cout << "Размер блока ввода-вывода: " << fileInfo.st_blksize << endl; | |
cout << "Количество выделенных блоков: " << fileInfo.st_blocks << endl; | |
strftime(buff, 100, "%A %d %b %Y %T", localtime(&fileInfo.st_atime)); | |
cout << "Время последнего доступа: " << buff << endl; | |
strftime(buff, 100, "%A %d %b %Y %T", localtime(&fileInfo.st_mtime)); | |
cout << "Время последнего доступа: " << buff << endl; | |
strftime(buff, 100, "%A %d %b %Y %T", localtime(&fileInfo.st_ctime)); | |
cout << "Время последнего доступа: " << buff << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
#define SIZE 512 | |
using namespace std; | |
int copy(char* copyFrom, char* copyTo) | |
{ | |
char buff1[SIZE] {}; // буффер для первого файла | |
char buff2[SIZE] {}; // буффер для второго файла | |
int file1 = open(copyFrom, O_RDWR); | |
int file2 = open(copyTo, O_RDWR); | |
read(file1, buff1, SIZE); // считываем первый файл в buff1 | |
cout << "Содержимое файла \"" << copyFrom << "\":" << endl; | |
cout << buff1; | |
strcpy(buff2, buff1); // копируем прочитанное из buff1 в buff2 | |
cout << "Производим копирование из \"" << copyFrom << "\" в \"" << copyTo << "\"" << endl; | |
write(file2, buff2, strlen(buff2)); // записываем во второй файл | |
cout << "Done..." << endl; | |
cout << "Содержимое файла \"" << copyTo << "\":" << endl; | |
read(file2, buff2, SIZE); | |
cout << buff2; | |
close(file1); | |
close(file2); | |
return 0; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
if (argv[1] != nullptr && argv[2] != nullptr) | |
copy(argv[1], argv[2]); | |
else | |
{ | |
cout << "Введите имя файла из которого планируем копировать:" << endl; | |
char copyFrom[10]; | |
cin >> copyFrom; | |
cout << "Введите имя файла куда планируем копировать:" << endl; | |
char copyTo[10]; | |
cin >> copyTo; | |
copy(copyFrom, copyTo); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <cstring> | |
#define ALL 0000700 | |
#define READ 0000400 | |
#define WRITE 0000200 | |
#define EXEC 0000100 | |
#define SIZE 512 | |
using namespace std; | |
int copy() | |
{ | |
int strLength; | |
char buf[SIZE]; | |
strLength = read(0, buf, SIZE); | |
write(1, buf, strLength); | |
return 0; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
int copyFrom; | |
int copyTo; | |
char cFname[10] {}; | |
char cTname[10] {}; | |
if (argv[1] == nullptr && argv[2] == nullptr) | |
{ | |
cout << "Пожалуйста, введите название файлов через пробел:" << endl; | |
cin >> cFname >> cTname; | |
copyFrom = open(cFname, O_RDONLY); | |
copyTo = open(cTname, O_WRONLY); | |
dup2(copyFrom, 0); | |
dup2(copyTo, 1); | |
copy(); | |
} | |
else | |
{ | |
copyFrom = open(argv[1], O_RDONLY); | |
copyTo = open(argv[2], O_WRONLY); | |
dup2(copyFrom, 0); | |
dup2(copyTo, 1); | |
copy(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment