mirror of
https://github.com/pkivolowitz/asm_book.git
synced 2026-06-21 03:36:49 +08:00
c version of file operations
This commit is contained in:
parent
b3045c07ba
commit
407faeea6f
1 changed files with 53 additions and 0 deletions
53
section_1/funcs/fops.c
Normal file
53
section_1/funcs/fops.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char * fname = "test.txt";
|
||||
char * p_legend = NULL;
|
||||
int fd = open(fname, O_RDWR | O_CREAT, 00600);
|
||||
int retval = 0;
|
||||
off_t seek_return;
|
||||
char buffer[80];
|
||||
|
||||
if (fd < 0) {
|
||||
p_legend = fname;
|
||||
goto out;
|
||||
} else {
|
||||
printf("Flags used to open file: %o\n", O_RDWR | O_CREAT);
|
||||
for (int counter = 0; counter < 10; counter++) {
|
||||
if (write(fd, "data\n", 5) != 5) {
|
||||
p_legend = "On writing";
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
if (seek_return = lseek(fd, 0, SEEK_SET) != 0) {
|
||||
p_legend = "On seeking";
|
||||
goto out;
|
||||
}
|
||||
|
||||
for (int counter = 0; counter < 10; counter++) {
|
||||
memset(buffer, 0, 80);
|
||||
if (read(fd, buffer, 5) != 5) {
|
||||
p_legend = "On reading";
|
||||
goto out;
|
||||
}
|
||||
printf("%d %s", counter, buffer);
|
||||
}
|
||||
|
||||
out: if (fd >= 0) {
|
||||
close(fd);
|
||||
if (unlink(fname)) {
|
||||
p_legend = "On unlinking";
|
||||
}
|
||||
}
|
||||
|
||||
if (p_legend != NULL) {
|
||||
perror(p_legend);
|
||||
retval = 1;
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
Loading…
Reference in a new issue