-2
#include <stdio.h>
int main (void)
{
    int col1, col2;
    FILE *fname = fopen("1930_2001.txt" , "r");
    fscanf(fname , "%d" , &col1);
}

Whenever I try to get information from the data file, before I even do that, I get an error that it can't access the information. It says it's "NULL" and I have the file in the debug folder in Xcode of my project. Any help?

RamonK
  • 7
  • Possible duplicate of [Error when trying to read in numbers from txt file in C](https://stackoverflow.com/questions/19148740/error-when-trying-to-read-in-numbers-from-txt-file-in-c) – l'L'l Mar 29 '18 at 03:35
  • when calling `fopen()`, always check (!=NULL) the returned value to assure the operation was successful. If not successful, call: `perror()` to output the enclosed text and the text of why the system thinks the operation failed to `stderr`, Then call `exit( EXIT_FAILURE );` to exit the program with an error indication – user3629249 Mar 29 '18 at 04:40
  • For further info: when calling any of the `scanf()` family of functions, 1) always check the returned value (not the parameter values) to assure the operation was successful. – user3629249 Mar 29 '18 at 04:41

1 Answers1

0

Have you put your data file and the compiled binary into the same folder? Xcode put the binary in somewhere not easy to find, you can Option+click the target name and choose "Show in Finder".

Or you can also compile your project using xcode-build CLI tool, or directly compile your program using clang, which saves you from lots of trouble.

BlueFlo0d
  • 180
  • 9