/** * Example of opening a file for reading and then reading from that file * * also fprintf * * @author gtowell * Created March 2021 * **/ #include #ifndef NULL #define NULL (void *) 0 #endif #define LINE_LEN 256 int main(int argc, char const *argv[]) { FILE *fInput = fopen("OpenClose.c", "r"); // any time you open a file check to make sure it opened!!!! if (NULL == fInput) { fprintf(stderr, "Failed to open file for reading ... terminating\n"); return 1; } // reading a file looks exactly line reading from stdin // indeed stdin is of type FILE* char line[LINE_LEN]; while (NULL != fgets(line, LINE_LEN, fInput)) { fprintf(stdout, "%s", line); } // if you open a file close it fclose(fInput); return 0; }