Mega Code Archive

 
Categories / C / Code Snippets
 

Get string from file

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *filep; char str[128]; if((filep = fopen(argv[ 1 ], "r"))==NULL) { printf("Cannot open file.\n"); exit(1); } while(!feof(filep)) { if(fgets(str, 126, filep)) printf("%s", str); } fclose(filep); return 0; }