diff --git a/Who_robbed_Thibouvre/fundamentals/basic_read/basic_read.c b/Who_robbed_Thibouvre/fundamentals/basic_read/basic_read.c index e69de29..c14df97 100644 --- a/Who_robbed_Thibouvre/fundamentals/basic_read/basic_read.c +++ b/Who_robbed_Thibouvre/fundamentals/basic_read/basic_read.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include +int open_close(const char *filename){ + + FILE *file = fopen(filename, "r"); + if(!file){ + //fclose(file) ; + return 0; + } + fclose(file); + return 1; +} + +int read_n_bytes(const char *filename, size_t n){ + + FILE *file = fopen(filename, "r"); + if (!file) return 1; + + char *cara = malloc((n + 1) * sizeof(char)); + if(!cara){ + fclose(file); + return 1; + } + + fread(cara, sizeof(char), n, file); + //cara[n+1] = '\0'; + //printf("%s\n", cara); + free(cara); + fclose(file); + return 0; +} + +int read_bytes(const char *filename){ + + FILE *file = fopen(filename, "r"); + if(!file) return 1; + + //fclose(file); + int c = fgetc(file); + int index = 0; + while(c != EOF){ + index ++; + c = fgetc(file); + } +/* + char *cara = malloc((index + 1) * sizeof(char)); + if(!cara) { + fclose(file); + return 1; + }*/ +/* + fread(cara, sizeof(char), index, file); + cara[index + 1] = '\0'; + printf("%s\n", cara); + free(cara); + fclose(file); +*/ + fclose(file); + int status = read_n_bytes(filename, index); + if(status == 0) return 0; + return 1; +} + +/* +$ cat does_exist.txt +Hey, as you can see, this file is not empty and therefore you can open it, read it, and close it! +*/ + +int main() +{ + int a = read_n_bytes("does_exist.txt", 32); + printf(" | %d\n", a); + /* Expected: + Hey, as you can see, this file i + | 0 + */ + + int b = read_n_bytes("does_not_exist.txt", 5); + printf(" | %d\n", b); + /* Expected: + | 1 + */ + a = read_bytes("does_exist.txt"); + printf("--> Return code: %d\n", a); + /* Expected: + Hey, as you can see, this file is not empty and therefore you can open it, read it, and close it! + --> Return code: 0 + */ + + b = read_bytes("does_not_exist.txt"); + printf("--> Return code: %d\n", b); + /* Expected: + --> Return code: 1 + */ + return 0; +} + diff --git a/Who_robbed_Thibouvre/fundamentals/basic_write/basic_write.c b/Who_robbed_Thibouvre/fundamentals/basic_write/basic_write.c index e69de29..d1b45a2 100644 --- a/Who_robbed_Thibouvre/fundamentals/basic_write/basic_write.c +++ b/Who_robbed_Thibouvre/fundamentals/basic_write/basic_write.c @@ -0,0 +1,31 @@ + + + +#include +int write_one_by_one(char *filename, const char *text){ + + FILE *file = fopen(filename, "rw"); + if(!file) return 1; + + + + return 0; +} + +int main() +{ + // does_exist.txt contains: "something which will be replaced" + int a = write_one_by_one("does_exist.txt", "Hello World!"); + // does_exist.txt contains: "Hello World!" + + // does_not_exist.txt... does not exist. + int b = write_one_by_one("does_not_exist.txt", "Now I exist"); + // does_not_exist.txt... now exists and contains: "Now I exist" + + printf("%d %d\n", a, b); // 0 0 + // The opening should succeed as it creates the file if needed + // However it can fail if the file's access is forbidden + + return 0; +} +