Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Using files with Xcode
    Hello guys
    I am a Windows convert now using mac!

    In my egr115 class (intro to computer programing.... and I am an Aerospace engineer major?!?!? :confused: ) We have been doing a lot of C programing, recently (in the last month or two) we have started to use files. When our class started using files I could not get them to work with xcode so I switched over to bloodshed on windows. Now our professor has assigned us a final project and I would really like to use my mac to do this. our final project has lots and lots of text files to read and write too.

    Lets say I write a program like this: (this real basic)

    #include <stdio.h>
    #include <stdlib.h>
    FILE *Fprint;
    Fprint = fopen("print.txt", "w");
    fprintf(Fprint, "hello");

    I have tried creating that text file by going to the menu and creating an empty text file and saving in the directory, I have even tried to save it has a txt file (by typing txt in while I save it) and it still will not read or write to any file.
    So here is my question;
    How do I use files with xcode? What do I use to create them? (I have been using xtool), what do I save them as (txt or let the computer handle it, then if I do that, how do I declare it?)

  2. #2

    Load Xcode as usual.

    Then File > New Project.

    Choose Command Line Utility, Foundation Tool.

    Double click the .m file that is created. .m is the extension of an Objective C file. This will open the editor:

    Then make sure the code is something like:

    #import <Foundation/Foundation.h>

    int main (int argc, const char * argv[])
    FILE *Fprint;
    Fprint = fopen("/print.txt", "w");
    fprintf(Fprint, "hello");

    Then build and run. print.txt will appear in the root of your Mac's hard disk.

    Objective C is C with OOP additions, so it will function just as the C language does, so don't worry about it being linked against the Objective C language, it's not the same as C++ which changes a few things around.

    You don't need to include the stdio and stdlib headers as the Foundation header sorts all this for.

    One book I would recommend is Stephen Kochan's Porgramming in Objective C book from Sams Developer Library series. It will teach you how to compile using XCode, the C language and C in the perspective of Objective C. Also recommended if you really want to get into the Mac side of things and Cocoa, Hillegass' Cocoa Programming in Mac OS X is *the* text.

    Hope this helps you.


  3. #3
    Also to add, #import work like #include with a difference - if the header is already included then it won't include it again, whereas #include will include the header regardless.

  4. #4
    Oh thanks but how do I send this to my professor who will read it in bloodshed? is this still c programing?

  5. #5
    Yes, you can of course use #include if you want.

    Just collect up the source files when you're done and email them over. I haven't used bloodshed but I shan't imagine it'd be too difficult for your lecturer to compile, C is C :flower:

    Besides it's in fact gcc (GNU C Compiler) beavering away behind the scenes for XCode :flower:


  6. #6
    I also need it to store these in a place that is easy to get to, also how do I bring up files? I would rather not store them there, I would like to store them in the project folder. Can I change the directory?

  7. #7
    Like I would like to store the files in the project folder (the folder xcode creates when you create a project)

  8. #8
    If you mean the main.m file etc., when you create a new project that is where it puts them, in that project folder.

    You can also use gcc on the command line and use a text editor of your choice if you want. If you would like that - give me a shout!

    Eventually though if you do want to develop Mac stuff say with Cocoa (ideally), learning XCode now would make sense, as you will come to find out!


  9. #9
    ^ ^

  10. #10
    I dont want my text files in the HArd disk folder, I want them close by the main.c. Also how do I bring up and use files that I will be reading and possible storing with xcode? do they have to be in the hard disk folder?

    Example (how would I make this work with xcode? how would I store the files and where??
    here is the timbers.txt File:

    16745 9000.0 120.0
    16789 20000.0 240.4
    22334 20000.0 60.0
    54321 16000.0 200.2
    12345 25000.0 150.5
    65123 10000.0 40.0
    77777 1000.0 22.6

    The timbout.txt is nothing until written upon.

    Here is the program:

    #include "stdio.h"
    #include "stdlib.h"
    #include "math.h"
    #define E 1700000

    //function declarations
    float buckling_load(float maximum_load, float area, float length, float width);
    float compressive_stress(float maximum_load, float area, float maximum_compressive_strength);
    float slenderness_limits(float length, float width);


    float maximum_load = 0; //starts out at zero
    float width=2;
    float length;
    float area=0;
    float true1=1;
    float true2=1;
    float true3=1;
    float maximum_compressive_strength = 445;
    int id=0;
    char answer=0;
    int TimberID[50];
    float Timberlength[50];
    float Timberload[50];
    int counter = 0;
    int c;

    FILE *timbers; //open files
    FILE *timbout;

    timbers = fopen("timbers.txt", "r");
    if (timbers == NULL)
    printf("sorry there is no file \n");
    system ("pause");

    while (fscanf(timbers,"%i",&id) != EOF )
    TimberID[counter] = id;
    Timberlength[counter] = length;
    Timberload[counter] = maximum_load;
    counter ++;

    //concludes true/else statement
    timbout = fopen("timbout.txt", "w+");
    fprintf(timbout, "%10s%10s%10s%8s x %-9s\n", "ID", "Length", "Load", "Width", "Length");
    for (c = 0; c < counter; c++)

    area=width*width; //declaring what area is
    true1 = buckling_load(Timberload[c], area, Timberlength[c], width); //everything is in order as they appear
    true2 = compressive_stress(Timberload[c], area,maximum_compressive_strength); //in order
    true3 = slenderness_limits(Timberlength[c], width); //in order as they appear

    /* if (
    /*(int)true1 == 0 && (int)true2 == 0 && (int)true3 == 0) //this true statment takes care of pass/fail
    /*printf("..%4.1f by %4.1f inch beam - PASSED the test \n", width, width);
    /* printf("..%4.1f by %4.1f inch bean - FAILED the test \n", width, width); */
    if (true1 == 0 && true2 == 0 && true3 == 0)
    fprintf(timbout, "%10i%10.1f%10.1f%8.1f x %-9.1f\n", TimberID[c], Timberlength[c], Timberload[c], width, length);

    }while (true1 !=0 || true2 !=0 || true3 !=0);

    }//end of main

    float buckling_load(float maximum_load, float area, float length, float width)
    /* a. Bucking load/
    /* maximum load = .30 x E x area (cross sectionalarea)/(length/width)^2 */
    float t_rue=1;
    float n = .30 * E * area;
    n /= ((length/width)*(length/width));
    } //end of if statement
    return t_rue; //returns the value of "true" to the main function
    } //end of buckling_load

    float compressive_stress(float maximum_load, float area, float maximum_compressive_strength)
    /* b. Compressice Stress/
    /* max load = area (cross sectional area) x maximum compressivestrenght */

    float tr_ue=1; //function names don't carry over...
    if(maximum_load<=area*maximum_compressive_strength )
    {tr_ue=0; //so you cant name things "true" who would of thought.....
    } //end of if statement
    return tr_ue; //see note in buckling load function
    } //end of compressive stress

    float slenderness_limits(float length, float width)
    /* c. Slenderness Limits/
    /* Length (length of column) / width <= 50*/

    float tru_e=1; // again, function names do not carry over
    if(length/width <= 50)
    {tru_e = 0;
    return tru_e;

  11. #11
    BTW that is a program I wrote that made me want to throw my powerbook against the wall because it would not compile and find the files. (they were in the folder!!!!) It was not until my roommate came with his windows machine that I was able to see it work.

  12. #12
    BTW2 the reason I bring up that program is because it has two files and one of them you read from, the other you write too.

  13. #13
    Quote Originally Posted by lil

    You can also use gcc on the command line and use a text editor of your choice if you want. If you would like that - give me a shout!

    Ok SHOUT! but what would that do?

    Im so confused..

  14. #14
    It just means you could compile your C programs from the terminal and set up your source files however you wanted without XCode organising stuff.

    Say you make a folder in your Documents folder in Finder and call it Source

    And then in there you save all your source code.

    In textedit you might have a simple program:

    /* basic C program */

    #include <stdio.h>

    int main(int argc, const char *argv[])
    printf("Hello World.\n");

    return 0;

    You save this in your Source folder as main.c

    Then you open Terminal, from Applications folder and then Utilities.

    At the prompt you type:

    cd documents/source

    Now you are in the source folder you made.

    You can check the main.c file is there by typing


    So it's there and we wish to compile it. So we use the gcc compiler that XCode uses:

    gcc main.c -o helloworld

    This will compile an app called helloworld in the current folder.

    Now because this folder is not in the system path, simply typing 'helloworld' at the prompt is not enough, as the system won't find it.

    So instead you would type (and press enter)


    ~/ means your home folder, or your user profiles folder.

    Does this make sense? :flower: You should then be able to compile away fine.


  15. #15
    NO!! Why is mac so confusing, on windows you can creat a project, build, compile and run from the same program and all your files will be saved with main.c. NOT in random places throughout the hard disk, so where would I store timbers.txt?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding subs to video files problem (huge converted files)
    By charadras in forum macOS - Apps and Games
    Replies: 0
    Last Post: 09-17-2014, 04:51 PM
  2. Replies: 2
    Last Post: 08-09-2013, 01:16 PM
  3. Replies: 1
    Last Post: 08-22-2011, 02:17 AM
  4. Replies: 6
    Last Post: 05-19-2011, 08:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts