Mega Code Archive

 
Categories / C / Code Snippets
 

Viewing the contents of a file

#include <stdio.h> #include <ctype.h> #include <string.h> #define DISPLAY 80 #define PAGE_LENGTH 20 int main(int argc, char *argv[]) { char filename[80]="c:\\myfile.txt"; FILE *pfile; unsigned char buffer[DISPLAY/4 - 1]; int count = 0; int lines = 0; int i = 0; if((pfile = fopen(filename, "rb")) == NULL){ printf("Sorry, can't open %s", filename); return -1; } while(!feof(pfile)) { if(count < sizeof buffer) /* If the buffer is not full */ printf("%c \n", (unsigned char)fgetc(pfile)); if(count < sizeof buffer) /* If the buffer is not full */ printf("%02X \n", (unsigned char)fgetc(pfile)); } fclose(pfile); return 0; }