Last active
March 2, 2018 14:55
-
-
Save menangen/9f729c1e7cac6b2074dce96444623c50 to your computer and use it in GitHub Desktop.
C file reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// fileIO.c | |
// hashreader | |
// | |
// Created by menangen on 01/03/2018. | |
// Copyright © 2018 Andrey Menangen. All rights reserved. | |
// | |
#include <stdlib.h> // malloc, free, exit | |
#include <stdio.h> // fprintf, perror, fopen, etc. | |
#include <string.h> // strlen, strcat, memset, strerror | |
#include <errno.h> // errno | |
#include <sys/stat.h> // stat | |
#include "utils.c" | |
static void test() { | |
printf("Hello, World!\n\n"); | |
} | |
static size_t fsize_orDie(const char *filename) | |
{ | |
struct stat st; | |
if (stat(filename, &st) == 0) return (size_t)st.st_size; | |
/* error */ | |
perror(filename); | |
exit(1); | |
} | |
static FILE* fopen_orDie(const char *filename, const char *instruction) | |
{ | |
FILE* const inFile = fopen(filename, instruction); | |
if (inFile) return inFile; | |
/* error */ | |
perror(filename); | |
exit(3); | |
} | |
static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file) | |
{ | |
size_t const readSize = fread(buffer, 1, sizeToRead, file); | |
return readSize; | |
/* error */ | |
perror("fread"); | |
exit(4); | |
} | |
static size_t fclose_orDie(FILE* file) | |
{ | |
if (!fclose(file)) return 0; | |
/* error */ | |
perror("fclose"); | |
exit(6); | |
} | |
static void readFile_orDie(const char* fileName) | |
{ | |
size_t read; | |
FILE* const fin = fopen_orDie(fileName, "rb"); | |
size_t const fileSize = fsize_orDie(fileName); | |
size_t const buffInSize = 50; | |
size_t const sizeToRead = 50; | |
char buffIn[buffInSize]; | |
while ((read = fread_orDie(buffIn, sizeToRead, fin)) > 0) | |
{ | |
// process bytesRead worth of data in buffer | |
if (read == sizeToRead) { | |
printf("Reading\n\n"); | |
} | |
else { | |
printf("End file\n\n"); | |
} | |
for (size_t count = 0; count < read; count++) { | |
printf("%i | ", buffIn[count]); | |
} | |
printf("\n\n"); | |
printf("Read chunk size: %lu\n\n", read); | |
} | |
printf("file size: %lu bytes\n", fileSize); | |
print_file_size(fileSize); | |
fclose_orDie(fin); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// hashreader | |
// | |
// Created by menangen on 01/03/2018. | |
// Copyright © 2018 Andrey Menangen. All rights reserved. | |
// | |
#include <stdio.h> | |
#include "fileIO.c" | |
int main(int argc, const char * argv[]) { | |
test(); | |
const char* const inFilename = "/Users/andrey/createEFI.sh"; | |
readFile_orDie(inFilename); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// utils.c | |
// hashreader | |
// | |
// Created by menangen on 02/03/2018. | |
// Copyright © 2018 Andrey Menangen. All rights reserved. | |
// | |
#include <stdio.h> | |
static void print_file_size(const size_t byte_size) | |
{ | |
const size_t KiloBytes = (byte_size >> 10) % 1024; | |
const size_t MegaBytes = (byte_size >> 20) % 1024; | |
const size_t GigaBytes = (byte_size >> 30) % 1024; | |
printf("File size:\n"\ | |
"%lu Gb %lu Mb %lu KBytes"\ | |
"\n", | |
GigaBytes, | |
MegaBytes, | |
KiloBytes | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment