#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
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;
}

