[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
Форум » Компьютеры » Программирование » C++, C# » Коды некоторых программ Linux
Коды некоторых программ Linux
АлексейДата: Воскресенье, 15.11.2015, 13:18 | Сообщение # 1
Продвигающийся
Группа: Администраторы
Сообщений: 324
Награды: 4
Репутация: 1
Статус: Оффлайн
Код под спойлер или в форму кода




Код
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{

printf("create file and open to read\n");
int u = (int)argv[2];
u=u-2;
int file = creat(argv[1], atoi(argv[2]));
  if (file == -1)
  {
    perror("Error while creating file: ");
    return 0;
  }

printf("write to the file \"many street\"\n");
char buff[100] = "one street \n two street";
write(file, buff, strlen(buff));
close(file); //Закрываем файл

//Открываем и считываем
char buff2[100];
file = open(argv[1], O_RDONLY);
read(file, buff2, strlen(buff));
close(file);

printf("read to file 1: %s\n", buff2);

printf("\nread file в режиме чтения\n");
buff2[0]='\0';
file = open(argv[1], O_RDONLY);
  if (file == -1)
  {
    perror("Error while opening file: ");
    return 0;
  }
read(file, buff2, strlen(buff));
printf("read file 2:\n %s\n", buff2);
close(file);

printf("\nread file в режиме чтения и записи\n"); 
char buff3[100];
buff3[0]='\0';
file = open(argv[1], atoi(argv[2]));
  if (file == -1)
  {
    perror("Error while opening file: ");
    return 0;
  }
read(file, buff3, strlen(buff));
printf("read file 3:\n %s\n", buff3);
close(file);
return(0);
}
 
АлексейДата: Воскресенье, 15.11.2015, 13:19 | Сообщение # 2
Продвигающийся
Группа: Администраторы
Сообщений: 324
Награды: 4
Репутация: 1
Статус: Оффлайн
Код#include #include #include #include #include #define BIG_SIZE 0x30

int seek(int file, int offset, int mode, int lenght){  if (lseek(file, offset, mode)==-1)  {    perror("Error while doing lseek: ");    return 1;  }  char buff[100]="\0";  read(file, buff, lenght);  printf("Длинна файла: %d \n", lenght);  printf("Вывод строки из файла:\n %s\n", buff);  return 0;}
int main(){
  printf("Создаем файл с правами на чтение и запись\n");  int file = open("nfile6", O_RDWR| O_CREAT | O_TRUNC, 444);  if (file == -1)  {    perror("Error while creating file: ");    return 0;  }  printf("Записываем в файл строку\n");  char buff[100] = "123456789";  write(file, buff, strlen(buff));  lseek(file, BIG_SIZE, SEEK_CUR);  write(file, buff, strlen(buff));  char buff2[100]="\0";  read(file, buff2, strlen(buff));  printf("Читаем содержимое файла nfile6: %s\n", buff);
  printf("SEEK_SET и смещением 5\n");  if (seek(file, 5, SEEK_SET, strlen(buff))==1) return 0;
  printf("SEEK_CUR, добавив смещение 2\n");  lseek(file, 5, SEEK_SET);  if (seek(file, 2, SEEK_CUR, strlen(buff))==1) return 0;
  printf("SEEK_END и смещением -5\n");  if (seek(file, -5, SEEK_END, strlen(buff))==1) return 0;  close(file);}
 
АлексейДата: Воскресенье, 15.11.2015, 13:20 | Сообщение # 3
Продвигающийся
Группа: Администраторы
Сообщений: 324
Награды: 4
Репутация: 1
Статус: Оффлайн
#include
#include
#include
#include
#include
#include

Код#include #include #include #include #include #include
int copyfile(char f1[100], char f2[100], int fff){  int file1=1;  int file2=0;  if (fff>1)  {    file1 = open(f1, O_RDONLY); if (fff>2) { file2 = open(f2, O_CREAT | O_RDWR | O_TRUNC, 777); if(file1 == -1 || file2 == -1) { perror("Error with one of the files: "); return 0; }  char buf[200] = "\0";  read(file1, buf, sizeof(buf));   write(file2, buf, sizeof(buf));    printf("Копирование файла %s в %s файл завершено\n",f1,f2);  close(file1);  close(file2); }  }}
int main(int argc, char *argv[]){  copyfile(argv[1],argv[2], argc);}
 
Форум » Компьютеры » Программирование » C++, C# » Коды некоторых программ Linux
  • Страница 1 из 1
  • 1
Поиск: