ifp = (file*)NULL
Thank you I had a feeling that might be it. That being said, I am very new to this, do you know how I can direct it to the proper location?
In addition to Lee's comments:So what about the rest?
Do I have the file in the wrong spot?
How do I get it to the right spot?
How would we know? Where did you put it?
Use Finder.
Again, how would we know what "the right spot" is? Did your professor tell you where they want you to put it?
As the code is written, it expects the file to be found in the same directory as the executable. Put it there.
Sorry to ask such a "did you plug it in" question, but....
To be fair, the file location isn't very straightforward when using Xcode
What does Xcode have to do with it? It's a tool for creating applications. When the application runs, it isn't using Xcode, and so Xcode has nothing to do with run-time file paths.
The path the text file needs to be in is different if you built the same source code file in Xcode as compared to the commandline.
I have an assignment that has to read in numbers from a file ("pizza.txt") and I keep getting the same error in green. Do I have the file in the wrong spot? How do I get it to the right spot? I'm completely new to this, please help!
Image
FILE *myFile;
myFile = fopen("myFile.txt", "r");
if (!myFile) {
// you dun goofed...
}
$ pwd
/Users/robert/Documents/fileio
Silver:fileio robert$ ls -l
total 40
-rwxr-xr-x 1 robert staff 8720 Oct 22 19:47 fileio
-rw-r--r-- 1 robert staff 379 Oct 22 19:47 fileio.c
-rw-r--r-- 1 robert staff 73 Oct 22 19:45 pizza.txt
Silver:fileio robert$ cat fileio.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE *fp;
fp = fopen("pizza.txt", "r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of the file are :\n");
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return 0;
}
Silver:fileio robert$ ./fileio
The contents of the file are :
This is a test file.
It has some text in it.
Testing 1 2 3 4 5
The End
$ find *
FileIO
FileIO/main.c
FileIO/pizza.txt
FileIO/TemplateIcon2x.png
FileIO.xcodeproj
FileIO.xcodeproj/project.pbxproj
FileIO.xcodeproj/project.xcworkspace
FileIO.xcodeproj/project.xcworkspace/contents.xcworkspacedata
FileIO.xcodeproj/project.xcworkspace/xcuserdata
FileIO.xcodeproj/project.xcworkspace/xcuserdata/robert.xcuserdatad
FileIO.xcodeproj/project.xcworkspace/xcuserdata/robert.xcuserdatad/UserInterfaceState.xcuserstate
FileIO.xcodeproj/xcuserdata
FileIO.xcodeproj/xcuserdata/robert.xcuserdatad
FileIO.xcodeproj/xcuserdata/robert.xcuserdatad/xcschemes
FileIO.xcodeproj/xcuserdata/robert.xcuserdatad/xcschemes/FileIO.xcscheme
FileIO.xcodeproj/xcuserdata/robert.xcuserdatad/xcschemes/xcschememanagement.plist
Silver:FileIO robert$ cd /Users/robert/Library/Developer/Xcode/
Silver:Xcode robert$ ls
Archives DeveloperPortal 5.0.2.db-shm DeveloperPortal 6.0.1.db-shm
DerivedData DeveloperPortal 5.0.2.db-wal DeveloperPortal 6.0.1.db-wal
DeveloperPortal 5.0.1.db DeveloperPortal 5.1.1.db UserData
DeveloperPortal 5.0.1.db-shm DeveloperPortal 5.1.1.db-shm iOS Device Logs
DeveloperPortal 5.0.1.db-wal DeveloperPortal 5.1.1.db-wal
DeveloperPortal 5.0.2.db DeveloperPortal 6.0.1.db
Silver:Xcode robert$ cd DerivedData/
Silver:DerivedData robert$ ls
FileIO-fevklprqpvjzsddtbvrrmvxyqcms ModuleCache Pixen-cmxgqbzpdbxqrmhjuzivurvzvyzl
Silver:DerivedData robert$ ls -l
total 0
drwxr-xr-x@ 8 robert staff 272 Oct 22 19:55 FileIO-fevklprqpvjzsddtbvrrmvxyqcms
drwxr-xr-x@ 5 robert staff 170 Oct 22 19:55 ModuleCache
drwxr-xr-x@ 9 robert staff 306 Jun 14 12:40 Pixen-cmxgqbzpdbxqrmhjuzivurvzvyzl
Silver:DerivedData robert$ cd FileIO-fevklprqpvjzsddtbvrrmvxyqcms/
Silver:FileIO-fevklprqpvjzsddtbvrrmvxyqcms robert$ ls -l
total 16
drwxr-xr-x 4 robert staff 136 Oct 22 19:54 Build
drwxr-xr-x 3 robert staff 102 Oct 22 19:54 Index
drwxr-xr-x 5 robert staff 170 Oct 22 19:56 Logs
drwxr-xr-x 3 robert staff 102 Oct 22 19:54 TextIndex
-rw-r--r-- 1 robert staff 278 Oct 22 19:54 info.plist
-rw-r--r-- 1 robert staff 230 Oct 22 19:54 scm.plist
Silver:FileIO-fevklprqpvjzsddtbvrrmvxyqcms robert$ cd Build
Silver:Build robert$ ls -l
total 0
drwxr-xr-x@ 3 robert staff 102 Oct 22 19:54 Intermediates
drwxr-xr-x@ 3 robert staff 102 Oct 22 19:55 Products
Silver:Build robert$ cd Products/
Silver:Products robert$ ls
Debug
Silver:Products robert$ cd Debug
Silver:Debug robert$ ls -l
total 24
-rwxr-xr-x 1 robert staff 9024 Oct 22 19:55 FileIO
Silver:Debug robert$ ./FileIO
Error while opening the file.
: No such file or directory
If you were to do this exercise from the Terminal, the code would work as expected.
Here you can see the directory I am in, and the files contained.
Code:$ pwd /Users/robert/Documents/fileio Silver:fileio robert$ ls -l total 40 -rwxr-xr-x 1 robert staff 8720 Oct 22 19:47 fileio -rw-r--r-- 1 robert staff 379 Oct 22 19:47 fileio.c -rw-r--r-- 1 robert staff 73 Oct 22 19:45 pizza.txt
Here's what the source code looks like:
Code:Silver:fileio robert$ cat fileio.c #include <stdio.h> #include <stdlib.h> int main() { char ch; FILE *fp; fp = fopen("pizza.txt", "r"); // read mode if( fp == NULL ) { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } printf("The contents of the file are :\n"); while( ( ch = fgetc(fp) ) != EOF ) printf("%c",ch); fclose(fp); return 0; }
Compiling the program from the Terminal is done with the command 'gcc fileio.c -o fileio', and then you run the program with the command ./fileio
Here's what you get by running the program:
Code:Silver:fileio robert$ ./fileio The contents of the file are : This is a test file. It has some text in it. Testing 1 2 3 4 5 The End
XCode, however, doesn't work that way. If you create a command-line application using the C language and add pizza.txt to that project, the same source code will not work. Let's see if we can find the executable.
Nowhere to be found in the project folder. What gives? Let's look around a little bit.Code:$ find * FileIO FileIO/main.c FileIO/pizza.txt FileIO/TemplateIcon2x.png FileIO.xcodeproj FileIO.xcodeproj/project.pbxproj FileIO.xcodeproj/project.xcworkspace FileIO.xcodeproj/project.xcworkspace/contents.xcworkspacedata FileIO.xcodeproj/project.xcworkspace/xcuserdata FileIO.xcodeproj/project.xcworkspace/xcuserdata/robert.xcuserdatad FileIO.xcodeproj/project.xcworkspace/xcuserdata/robert.xcuserdatad/UserInterfaceState.xcuserstate FileIO.xcodeproj/xcuserdata FileIO.xcodeproj/xcuserdata/robert.xcuserdatad FileIO.xcodeproj/xcuserdata/robert.xcuserdatad/xcschemes FileIO.xcodeproj/xcuserdata/robert.xcuserdatad/xcschemes/FileIO.xcscheme FileIO.xcodeproj/xcuserdata/robert.xcuserdatad/xcschemes/xcschememanagement.plist
Well, there it is. We could move pizza.txt to that location. You can change where the executable goes by going to Project Settings under the File menu in Xcode. You can also right-click on the program icon under products in XCode to see where the file is.Code:Silver:FileIO robert$ cd /Users/robert/Library/Developer/Xcode/ Silver:Xcode robert$ ls Archives DeveloperPortal 5.0.2.db-shm DeveloperPortal 6.0.1.db-shm DerivedData DeveloperPortal 5.0.2.db-wal DeveloperPortal 6.0.1.db-wal DeveloperPortal 5.0.1.db DeveloperPortal 5.1.1.db UserData DeveloperPortal 5.0.1.db-shm DeveloperPortal 5.1.1.db-shm iOS Device Logs DeveloperPortal 5.0.1.db-wal DeveloperPortal 5.1.1.db-wal DeveloperPortal 5.0.2.db DeveloperPortal 6.0.1.db Silver:Xcode robert$ cd DerivedData/ Silver:DerivedData robert$ ls FileIO-fevklprqpvjzsddtbvrrmvxyqcms ModuleCache Pixen-cmxgqbzpdbxqrmhjuzivurvzvyzl Silver:DerivedData robert$ ls -l total 0 drwxr-xr-x@ 8 robert staff 272 Oct 22 19:55 FileIO-fevklprqpvjzsddtbvrrmvxyqcms drwxr-xr-x@ 5 robert staff 170 Oct 22 19:55 ModuleCache drwxr-xr-x@ 9 robert staff 306 Jun 14 12:40 Pixen-cmxgqbzpdbxqrmhjuzivurvzvyzl Silver:DerivedData robert$ cd FileIO-fevklprqpvjzsddtbvrrmvxyqcms/ Silver:FileIO-fevklprqpvjzsddtbvrrmvxyqcms robert$ ls -l total 16 drwxr-xr-x 4 robert staff 136 Oct 22 19:54 Build drwxr-xr-x 3 robert staff 102 Oct 22 19:54 Index drwxr-xr-x 5 robert staff 170 Oct 22 19:56 Logs drwxr-xr-x 3 robert staff 102 Oct 22 19:54 TextIndex -rw-r--r-- 1 robert staff 278 Oct 22 19:54 info.plist -rw-r--r-- 1 robert staff 230 Oct 22 19:54 scm.plist Silver:FileIO-fevklprqpvjzsddtbvrrmvxyqcms robert$ cd Build Silver:Build robert$ ls -l total 0 drwxr-xr-x@ 3 robert staff 102 Oct 22 19:54 Intermediates drwxr-xr-x@ 3 robert staff 102 Oct 22 19:55 Products Silver:Build robert$ cd Products/ Silver:Products robert$ ls Debug Silver:Products robert$ cd Debug Silver:Debug robert$ ls -l total 24 -rwxr-xr-x 1 robert staff 9024 Oct 22 19:55 FileIO Silver:Debug robert$ ./FileIO Error while opening the file. : No such file or directory
Image