//* Determining the file size and reading from a binary file to memory *

#ifdef _MSC_VER // for MS Visual Studio compilers - to allow "classic" insecure printf and scanf #define _CRT_SECURE_NO_WARNINGS #endif #include <stdio.h> #include <stdlib.h> int main() { FILE *fileFile; char fileName[80]; int fileSize; char *buffer; printf("Read text from: "); scanf(" %79[^\n]s\n", fileName); fileFile = fopen(fileName, "rb"); // read in binary mode if ( fileFile==0 ) { printf("ERROR: Cannot open file %s for reading.\n", fileName); return(1); } fseek(fileFile, 0L, SEEK_END); fileSize = ftell(fileFile); buffer = malloc(fileSize); fseek(fileFile, 0L, SEEK_SET); fread(buffer, fileSize, 1, fileFile); fclose(fileFile); { int i; for (i=0; i<fileSize; i++) if (buffer[i]>=' ') printf("%4d %3d %c\n", i, (int)buffer[i], buffer[i] ); else printf("%4d %3d\n", i, (int)buffer[i] ); } free(buffer); return(0); }