Jump to content

New to OS X, why wont this work?


Chris_F
 Share

2 posts in this topic

Recommended Posts

Ok, here is the first program:

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
 char name[64];
 printf("Please enter your name: ");
 fgets(name, 64, stdin);
 name[strlen(name)-1] = 0;
 printf("Hello, %s!\n", name);
 exit(0);
}

 

Works fine on my computer, but my friend gets "Bus error".

 

He has Tiger on an intel mac, I have Leopard, but I doubt that should make a difference, it would be crazy if it did.

 

Second:

 

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
 FILE *fp;
 fp = fopen("testfile.txt", "w");
 fclose(fp);
 exit(0);
}

 

If I run it in terminal with the command ./programname it works fine, it creates a new file. But if I double click the program it only brings up terminal and finishes without making the file.

 

This just seen completely wacky. What's up?

Link to comment
Share on other sites

1) The operating system does make a difference. Chances are you built it for the default 10.5 SDK, and not the 10.4 SDK. Go to Project -> Edit Project Settings, and look under the General tab.

 

2) When you double click the program — or even if you run it manually through terminal — it treats the current directory (pwd in terminal) as the working directory, so your file is created there. By changing directories (cd) into programname's path and doing ./programname, you change the current working directory.

Link to comment
Share on other sites

 Share

×
×
  • Create New...