2

I am trying to write a file to open a file and read the contents of that file in C. I am using xcode but the file pointer returns a value null.

int main(int argc, char** argv)
{    
    FILE *fp;
    fp=fopen("input.txt","r");
    if (fp==NULL)
        printf("error");
}

This shows error as the output. Could someone help me to find the right place to put the file input.txt in the project?

Jörgen Sigvardsson
  • 4,839
  • 3
  • 28
  • 51
noddy
  • 1,010
  • 4
  • 14
  • 27

5 Answers5

6

The file would need to go in the "current directory" where the program is launched. This is configurable. In the "Groups and Files" pane, expand "Executables" and pick your executable. Press Command-I to open the info window. Near the bottom of the "General" tab, you can select the current directory for when the application is launched.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

In your file list on the left right click on your product (executable) and click on "Show in Finder". Paste the file you want to open in your program there.

vivek241
  • 666
  • 1
  • 7
  • 18
1

This is somewhat related, but if you want to open a file in iOS, then get the file path via the following:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Jon" ofType:@"CSV"];

FILE *file = fopen([filePath cStringUsingEncoding:NSUTF8StringEncoding], "r");
Jon
  • 7,848
  • 1
  • 40
  • 41
0

The file is not in the working directory or you don't have the right to read it.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
-1

i got the answer .I needed to store the file in the debug folder because xcode runs the file from the debug folder which is

noddy
  • 1,010
  • 4
  • 14
  • 27