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:
Thanks.
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");
exit(EXIT_FAILURE);
}
/* 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 */
fclose(fp);
printf("%d points output to file.\n", numPoints);
return 0;
}
Thanks.