-2

I just started to learn C and came across this error when trying to run a simple program to open a .txt file.

Sublime text editor code

MAC Terminal

6
  • 7
    fopen() can return error, so check for that. And put code and error in the post rather than screenshots.
    – Rohan
    Sep 25, 2017 at 4:52
  • 1
    Further, you are using "stdio.h" instead of <stdio.h>.
    – Simon Woo
    Sep 25, 2017 at 5:44
  • 1
    Please make your answer complete by providing your code and the error message. Not as external links and not as pictures, please.
    – Yunnosch
    Sep 25, 2017 at 6:46
  • Print the return value of fopen() before trying to use it. It wouldn't happen to be zero, would it? Read a documenation of fopen(). It is what Rohan already tried to make you aware of.
    – Yunnosch
    Sep 25, 2017 at 6:48
  • 1
    Why am I seeing test.dat in the code when you state open a .txt file in your question?
    – iBug
    Sep 25, 2017 at 7:40

1 Answer 1

0

Before trying to iterate through your file, test the pointer returned by fopen() :

if(!pToFile) // or if(pToFile == NULL) 
    exit(1); //exit the program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.