Mac C Programming - reading in file in Xcode

sembem

macrumors newbie
Original poster
Oct 13, 2014
2
0
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!:eek:

 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
Just quickly looking it over I noticed at the bottom it says
ifp = (file*)NULL
NULL means it doesn't exist. I don't see a path to the file? I'm pretty sure you are getting a NULL because you have not told it to look in the correct area for the file.
 

sembem

macrumors newbie
Original poster
Oct 13, 2014
2
0
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
8,782
5,165
vertical
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 your code, "pizza.txt" is going to be relative to the current working directory, so you need to set the working directory to the directory where "pizza.txt" is located. Exactly how and where you do that depends on which version of Xcode you have.

See here for Xcode's "Scheme Editor":
https://developer.apple.com/library...e.html#//apple_ref/doc/uid/TP40010402-CH8-SW1

Also search Xcode's builtin Help (Help menu) for info on using the Scheme Editor. Look under "Options" for a "Working directory" option, for example.


To understand working directory, see here:
http://en.wikipedia.org/wiki/Working_directory

Note that "pizza.txt" is a relative path, so read about that, too:
http://en.wikipedia.org/wiki/Path_(computing)


Finally, you need to add some code that checks whether ifp is NULL or not after fopen(). If it's NULL, then don't do the loop, because the file you asked for couldn't be opened.

The error-code identifying why the file wasn't opened will be in errno. See the man page for fopen() for details:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/fopen.3.html

These checks are called "error checking".
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
Others have responded to the actual file issue, which is almost certainly caused by pizza.txt being somewhere other than the working directory.

So what about the rest? For one, money isn't floating point in most contexts. Change all of your constants at the top and put the value in (), ie (699). Sales tax will need to remain a decimal. Now you can do integer math all the way. Currently you do some double precision math, then truncate the cents when you implicitly cast back to an int. Once you have the total price in cents, you can get the "dollar part" by dividing by 100, and the cents using % 100. The sales tax is a bit of a dilemma because you'll get fractional cents. If you want to deal with this you can, but for this project you can likely truncate and be OK (sometimes you'll be a cent too low).

Otherwise, rename every single variable. They are not meaningful. numSmallPizzas means something. s does not.

I know you're just starting and this seems overboard, but this is the easiest time to learn things that will save you in more complex programs in the future.

-Lee
 

briloronmacrumo

macrumors 6502
Jan 25, 2008
486
297
USA
So what about the rest?
In addition to Lee's comments:

The code declares variables as integers( int n, k, S, M, L, X, Y; ). Make sure ( i.e. go back to your instructor and ask or review notes for the lecture about how integers can be used ) you understand how variables are used. An integer variable may only contain a whole number with *no* fractional ( i.e decimal or if using US Dollars, cents ) piece. Lee's suggestion to do all calculations in cents allows integers to be used ( although depending on the size of the numbers larger integer variables such as UInt64 might be required ) but there are other ways.

Above all, while you're learning, make sure you can explain to yourself what each line of code does. Copying code and hoping it will work might get through a course but you won't learn as much.
 

jtara

macrumors 68000
Mar 23, 2009
1,957
509
Do I have the file in the wrong spot?
How would we know? Where did you put it?

How do I get it to the right spot?
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....
 

robvas

macrumors 68040
Mar 29, 2009
3,061
519
USA
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
 

jtara

macrumors 68000
Mar 23, 2009
1,957
509
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.
 

robvas

macrumors 68040
Mar 29, 2009
3,061
519
USA
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.
 

jtara

macrumors 68000
Mar 23, 2009
1,957
509
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.
How so? (Notwithstanding that Xcode may have some default place to create your project.)

If you know the answer, why not help the OP and provide it?
 

joshlalonde

macrumors 6502
Jul 12, 2014
422
0
Canada
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!:eek:

Image
Hey, not sure if I'm late to the party.
Just wanted to say that you should always make sure that you check to make sure a file actually opened.

It's as simple as using an if statement; something like:

Code:
FILE *myFile;

myFile = fopen("myFile.txt", "r");

if (!myFile) {
   // you dun goofed...
}
I'm almost certain that would work. I don't remember how C handles files and exceptions. But basically, fopen() should return something. If it's not the value you expect, then something's wrong..
 

robvas

macrumors 68040
Mar 29, 2009
3,061
519
USA
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.

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
Nowhere to be found in the project folder. What gives? Let's look around a little bit.
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
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.

 

joshlalonde

macrumors 6502
Jul 12, 2014
422
0
Canada
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.

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
Nowhere to be found in the project folder. What gives? Let's look around a little bit.
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
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.

Image
Thank you for doing error checking, sir and answering the question completely.:) +1 to you, have a good day.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.