This commit is contained in:
2025-11-27 16:29:23 +00:00
parent 9936cb924f
commit d2382a36dc
2 changed files with 39 additions and 0 deletions

30
pa/lab3/read_file.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
char file_to_read[64] = {0};
printf("Insert filename to read: ");
fflush(stdout);
int res = read(STDIN_FILENO, file_to_read, sizeof(file_to_read));
file_to_read[res-1] = '\x00';
if(!access(file_to_read, R_OK)) {
FILE *fd;
char buffer[2048] = {0};
puts("Content: \n===========================\n");
fd = fopen(file_to_read, "r");
fread(buffer, sizeof(buffer), 1, fd);
printf("%s\n", buffer);
puts("===========================");
} else {
printf("No permission to read '%s'\n", file_to_read);
}
return 0;
}