File IO Operations.

This commit is contained in:
Saturneric 2020-12-05 16:31:17 +08:00
parent c8a79b8066
commit d33313e906
8 changed files with 226 additions and 0 deletions

46
3/file/fileappend.c Normal file
View File

@ -0,0 +1,46 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int readFile(int fd){
char buff[1024];
ssize_t rbit = pread(fd, buff, 1024, 0);
if(rbit < 0){
printf("Error in reading file\n");
return -1;
}
buff[rbit] = '\0';
printf("rbit: %ld %s\n", rbit, buff);
return 0;
}
int main(void){
int fd = open("./test/fileat", O_RDWR | O_SYNC);
if(fd < 0){
printf("Error in opening file fileat.\n");
return -1;
}
readFile(fd);
ssize_t wbit = pwrite(fd, "How", 3, 0);
if(wbit < 0) {
printf("Error in writing file.\n");
return -1;
}
readFile(fd);
close(fd);
return 0;
}

30
3/file/filecntl.c Normal file
View File

@ -0,0 +1,30 @@
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int getFL(int fd) { return fcntl(fd, F_GETFL, 0); }
int main(void) {
int fd = open("./test/fileat", O_RDWR);
printf("FL: %d\n", getFL(fd));
if (fcntl(fd, F_SETFL, getFL(fd) | O_APPEND) < 0) {
printf("fcntl F_SETFL error\n");
return -1;
}
printf("FL: %d\n", getFL(fd));
char buff[] = "Nope";
int wbit = write(fd, buff, sizeof(buff));
if (wbit < 0) {
printf("write buff error\n");
return -1;
}
close(fd);
return 0;
}

31
3/file/filedup.c Normal file
View File

@ -0,0 +1,31 @@
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
int fd = open("./test/fileat", O_RDWR | O_SYNC);
int fd2 = dup(fd);
off_t off = lseek(fd, 3, SEEK_SET);
if (off == -1) {
printf("Fail to call lseek\n");
return -1;
}
char buff[1024];
int rbit = read(fd2, buff, 1024);
if (rbit < 0) {
printf("Error in reading file fileat");
return -1;
}
buff[rbit] = 0;
printf("rbit: %ld %s\n", rbit, buff);
return 0;
}

33
3/file/filerw.c Normal file
View File

@ -0,0 +1,33 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main(void){
int fd = open("./test/fileat", O_RDWR | O_SYNC);
if(fd == -1){
printf("Error in opening file fileat.\n");
}
char buff[1024];
ssize_t rbit = read(fd, buff, 1024);
if(rbit > 0)
buff[rbit] = '\0';
printf("%ld %s\n", rbit, buff);
off_t off = lseek(fd, 0, SEEK_END);
printf("offset: %ld\n", off);
char buff2[32] = "Hello World!";
ssize_t wbit = write(fd, buff2, 13);
printf("wbit: %ld\n", wbit);
return 0;
}

51
3/file/filesync.c Normal file
View File

@ -0,0 +1,51 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int readFile(){
int fd2 = open("./test/fileat", O_RDONLY);
if(fd2 < 0){
printf("Error in opening file fileat\n");
return -1;
}
char buff2[1024];
ssize_t rbit = read(fd2, buff2, 1024);
if(rbit < 0){
printf("Error in reading file fileat\n");
return -1;
}
buff2[rbit] = 0;
printf("rbit: %ld %s", rbit, buff2);
close(fd2);
return 0;
}
int main(void){
int fd = open("./test/fileat", O_RDWR | O_APPEND);
char buff[] = "Helloworld";
ssize_t wbit = write(fd, buff, sizeof(buff));
if(wbit != sizeof(buff)){
printf("Error occur when wrote file fileat\n");
return -1;
}
readFile();
fsync(fd);
readFile();
close(fd);
return 0;
}

35
3/file/openfile.c Normal file
View File

@ -0,0 +1,35 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#define RWRR S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
int main(void) {
int fd = open("./test/file", O_RDONLY | O_CREAT, RWRR);
int fd2 = open("./test/", O_DIRECTORY | O_EXCL);
printf("%d\n", fd);
printf("%d\n", fd2);
int fd3 = openat(fd2, "./fileat", O_TRUNC | O_RDWR | O_CREAT | O_SYNC, RWRR);
int fd4 = creat("./test/filecreat", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
off_t off = lseek(fd4, 8, SEEK_SET);
off = lseek(fd4, -2, SEEK_CUR);
off = lseek(fd4, 1024, SEEK_CUR);
printf("%ld", off);
close(fd4);
close(fd3);
close(fd2);
close(fd);
return 0;
}

BIN
3/file/test/fileat Normal file

Binary file not shown.

0
3/file/test/filecreat Normal file
View File