Start on FILE i/O
This commit is contained in:
1 parent
7033104799
commit
03518c4937
22 files changed
+330
-127
No files matched your search
+63
@@ -0,0 +1,63 @@
|
||||
#include "file.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Up to 256 files can be open
|
||||
FILE *files[256];
|
||||
|
||||
int openFile(int fd, const char *path) {
|
||||
files[fd] = fopen(path, "r+");
|
||||
if (files[fd] == NULL) {
|
||||
return errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int closeFile(int fd) {
|
||||
int ret = fclose(files[fd]);
|
||||
if (ret != 0) {
|
||||
return errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int readByte(const int fd, int *data) {
|
||||
int status = 0;
|
||||
int count = fread(&data, 1, 1, files[fd]);
|
||||
// Error or EOF
|
||||
if (count != 1) {
|
||||
int error = ferror(files[fd]);
|
||||
int eof = feof(files[fd]);
|
||||
if (error != 0) {
|
||||
status = 0x2;
|
||||
} else if (eof != 0) {
|
||||
status = 0x1;
|
||||
} else {
|
||||
status = 0x4;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int writeByte(const int fd, const int data) {
|
||||
int status = 0;
|
||||
int count = fwrite(&data, 1, 1, files[fd]);
|
||||
// Error or EOF
|
||||
if (count != 1) {
|
||||
int error = ferror(files[fd]);
|
||||
int eof = feof(files[fd]);
|
||||
if (error != 0) {
|
||||
status = 0x2;
|
||||
} else if (eof != 0) {
|
||||
status = 0x1;
|
||||
} else {
|
||||
status = 0x4;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
Reference in new issue
Block a user