How to read from a text file I made in C -


i new c, trying read simple text file created in c. made file clicking new -> empty file -> saving desired location , adding file extension (.txt) text file holds sample sudoku board , full file name sudokuchar.txt.

the code have read file , print is:

#include <stdio.h> #include <stdlib.h>  int main() {     file *fpointer = fopen("sudokuchar.txt", "r");     char input[100];     while(fgets(input,100,fpointer))     {         printf("%s",input);     }     fclose(fpointer); } 

so when compile program not print , returns -1. assume wrong file trying read from?? if 1 appreciated.

always check return values of fopen , other standard library calls. it's file doesn't exist. can make nice user friendly error message using errno, make sure include errno.h. overall, code should work, need check return values of things, because fopen returns null if can't find file.

file *fpointer = fopen("sudokuchar.txt", "r");  if(fpointer == null) {     fprintf(stderr, "error: [errno %d]: %s\n",              errno, strerror(errno));     return 1; } 

Comments