- •Лабораторная работа №1
- •Программа вывода сообщения на экран
- •Написать программу ввода символов с клавиатуры и записи их в файл
- •Написать программу вывода содержимого текстового файла на экран
- •Написать программу копирования одного файла в другой.
- •Написать программу вывода на экран содержимого текущего и заданного первым параметром вызова программы каталогов.
- •Индивидуальное задание
Министерство цифрового развития, связи и массовых коммуникаций Российской Федерации
Ордена Трудового Красного Знамени федеральное государственное бюджетное образовательное учреждение высшего образования
«Московский технический университет связи и информатики»
(МТУСИ)
Кафедра «Математическая кибернетика и информационные технологии»
Лабораторная работа №1
на тему
«Использование языка программирования C для работы в ОС Linux»
Выполнил:
Студент 1 курса магистратуры
Группы М092201(75)
Францев Артем
Проверил:
Симонов Сергей Евгеньевич
Москва 2022
Программа вывода сообщения на экран
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
Написать программу ввода символов с клавиатуры и записи их в файл
#include <stdio.h>
int main(int argc, char *argv[])
{
// Check if a file name was provided as an argument
if (argc < 2)
{
printf("Error: please provide a file name as an argument.\n");
return 1;
}
// Open the file for writing
FILE *file = fopen(argv[1], "w");
if (file == NULL)
{
printf("Error: unable to open file for writing.\n");
return 1;
}
// Read characters from the keyboard and write them to the file
int c;
while ((c = getc(stdin)) != EOF)
{
// Check for the exit combination (ctrl-F)
if (c == 6)
{
break;
}
// Write the character to the file
fputc(c, file);
}
// Close the file
if (fclose(file) != 0)
{
printf("Error: unable to close file.\n");
return 1;
}
return 0;
}
Написать программу вывода содержимого текстового файла на экран
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// Check if a file name and N were provided as arguments
if (argc < 3)
{
printf("Error: please provide a file name and N as arguments.\n");
return 1;
}
// Parse the second argument (N) as an integer
int N = atoi(argv[2]);
// Open the file for reading
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("Error: unable to open file for reading.\n");
return 1;
}
// Read and print the contents of the file
int c;
int line_count = 0;
while ((c = fgetc(file)) != EOF)
{
// Print the character to the screen
putc(c, stdout);
// If N is not 0, check if we have reached the end of a group of N lines
if (N != 0)
{
line_count++;
if (line_count == N)
{
// Wait for the user to press a key before printing the next group of lines
printf("\n--- Press any key to continue ---");
getc(stdin);
// Reset the line count
line_count = 0;
}
}
}
// Close the file
if (fclose(file) != 0)
{
printf("Error: unable to close file.\n");
return 1;
}
return 0;
}
Написать программу копирования одного файла в другой.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
// Check if the source and destination file names were provided as arguments
if (argc < 3)
{
printf("Error: please provide the source and destination file names as arguments.\n");
return 1;
}
// Open the source file for reading
FILE *src = fopen(argv[1], "r");
if (src == NULL)
{
printf("Error: unable to open source file for reading.\n");
return 1;
}
// Open the destination file for writing
FILE *dest = fopen(argv[2], "w");
if (dest == NULL)
{
printf("Error: unable to open destination file for writing.\n");
fclose(src);
return 1;
}
// Copy the file permissions from the source file to the destination file
struct stat src_stat;
if (fstat(fileno(src), &src_stat) != 0)
{
printf("Error: unable to get file permissions for source file.\n");
fclose(src);
fclose(dest);
return 1;
}
if (fchmod(fileno(dest), src_stat.st_mode) != 0)
{
printf("Error: unable to set file permissions for destination file.\n");
fclose(src);
fclose(dest);
return 1;
}
// Copy the contents of the source file to the destination file
int c;
while ((c = fgetc(src)) != EOF)
{
fputc(c, dest);
}
// Close the files
if (fclose(src) != 0)
{
printf("Error: unable to close source file.\n");
fclose(dest);
return 1;
}
if (fclose(dest) != 0)
{
printf("Error: unable to close destination file.\n");
return 1;
}
return 0;
}