C Programming - reading in file in Xcode

Discussion in 'Mac Programming' started by sembem, Oct 13, 2014.

  1. sembem macrumors newbie

    Joined:
    Oct 13, 2014
    #1
    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:

    [​IMG]
     
  2. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    Just quickly looking it over I noticed at the bottom it says
    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.
     
  3. sembem thread starter macrumors newbie

    Joined:
    Oct 13, 2014
    #3
    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?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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".
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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
     
  6. briloronmacrumo macrumors 6502

    briloronmacrumo

    Joined:
    Jan 25, 2008
    Location:
    USA
    #6
    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.
     
  7. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #7
    This is a project that's easier to do from Terminal than Xcode
     
  8. jtara macrumors 65816

    Joined:
    Mar 23, 2009
    #8
    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....
     
  9. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #9
    To be fair, the file location isn't very straightforward when using Xcode
     
  10. jtara macrumors 65816

    Joined:
    Mar 23, 2009
    #10
    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.
     
  11. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #11
    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.
     
  12. jtara macrumors 65816

    Joined:
    Mar 23, 2009
    #12
    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?
     
  13. joshlalonde macrumors 6502

    joshlalonde

    Joined:
    Jul 12, 2014
    Location:
    Canada
    #13
    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..
     
  14. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #14
    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.

    [​IMG]
     
  15. joshlalonde macrumors 6502

    joshlalonde

    Joined:
    Jul 12, 2014
    Location:
    Canada
    #15
    Thank you for doing error checking, sir and answering the question completely.:) +1 to you, have a good day.
     

Share This Page