Xcode file output

Discussion in 'Mac Programming' started by xcodeROOKIE, Oct 3, 2013.

  1. xcodeROOKIE macrumors newbie

    Oct 3, 2013
    Hey guys, I'm having problem outputting to a file in Xcode. Im programming with C, using the command line tool template. Im trying to implement the algorithm to generate pascal's triangle and print it to a .txt file. My code builds and runs fine and will generate appropriate coordinates if i were to print the last pair out using printf, however theres no file being generated. Ive searched for it using finder as well. Here is my code:

    #include <stdio.h>
    #include <stdlib.h>
    #define FILENAME "points.txt"
    typedef struct {
    	double x;
    	double y;
    } Point;
    int main(void)
    	FILE *fp;
    	Point a, b, c, d;
    	int i, numPoints, pick;
    	/* Initialise the positions of points a, b and c */
    	a.x = 100;
    	a.y = 173;
    	b.x = 0;
    	b.y = 0;
    	c.x = 200;
    	c.y = 0;
    	/* Read the number of points to plot */
    	printf("Enter number of points: ");
    	scanf("%d", &numPoints);
    	/* Initially point d is at some random location */
    	d.x = (double)(rand() % 100);
    	d.y = (double)(rand() % 100);
    	fp = fopen(FILENAME, "w");
    	if (fp == NULL) {
    		printf("Error opening file.\n");
    	/* Output the initial three points */
    	fprintf(fp, "%f %f\n", a.x, a.y);
    	fprintf(fp, "%f %f\n", b.x, b.y);
    	fprintf(fp, "%f %f\n", c.x, c.y);
    	fprintf(fp, "%f %f\n", d.x, d.y);
    	/* Calculate next point according to algorithm */
        // let a=0, b=1, c=2
        for (i=0; i<numPoints; i++) {
            pick = (rand() % (3));
            if (pick == 0) {
                d.x = (d.x+a.x)/2;
                d.y = (d.y+a.y)/2;
                fprintf(fp, "%f %f\n", d.x, d.y);
            else if (pick == 1) {
                d.x = (d.x+b.x)/2;
                d.y = (d.y+b.y)/2;
                fprintf(fp, "%f %f\n", d.x, d.y);
            else {
                d.x = (d.x+c.x)/2;
                d.y = (d.y+c.y)/2;
                fprintf(fp, "%f %f\n", d.x, d.y);
    	/* Close the file */
    	printf("%d points output to file.\n", numPoints);
    	return 0;
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    Where are you expecting the file? If you are running from within XCode do you know the cwd?
  3. robvas macrumors 68030

    Mar 29, 2009
  4. mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    If you run the program from Xcode, your current working directory will be the build folder for your executable deep inside the Library folder in your home directory. Finder won't find that folder by default. You'll have to find it with the Terminal. The project settings will contain the build output folder. That's where your output file is going since you didn't specify a full path.
  5. xcodeROOKIE thread starter macrumors newbie

    Oct 3, 2013
    Ok, I probably should have said that i'm very new to programming, especially with Xcode. So I've got the following questions:
    1) How do you find your current working directory? ( I thought it was just where your source code was stored??)
    2) What do you mean by terminal? At present I just click the big "play" button.
    3) How do you specify where the file will be stored? Ive gone into project settings, followed the derived data location link but it just comes up with a bunch of folders all of which don't lead to my output file.
    You'll probably think i'm an idiot now for asking what is presumably the most basic questions its just that I don't have this issue when using visual stdio at uni as the output file is found next to the project file.


Share This Page