Mega Code Archive

 
Categories / C / Code Snippets
 

File content seek

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *filep; if(argc!=3) { printf("Usage: SEEK filename byte\n"); exit(1); } if((filep = fopen(argv[1], "rb"))==NULL) { printf("Cannot open file.\n"); exit(1); } if(fseek(filep, atol(argv[2]), SEEK_SET)) { printf("Seek error.\n"); exit(1); } printf("Byte at %ld is %c.\n", atol(argv[2]), getc(filep)); fclose(filep); return 0; }