New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Using files with Xtool


Post Reply New Thread Subscribe

 
Thread Tools
Sloopy
Guest
 
Posts: n/a

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>
main()
{
FILE *Fprint;
Fprint = fopen("print.txt", "w");
fprintf(Fprint, "hello");
fclose(Fprint);
return(0)
}

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?)
Thanks
-Bill
QUOTE Thanks
lil
Guest
 
Posts: n/a

Hiya,

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");
fclose(Fprint);
return(0);
}
--

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.

Vicky
QUOTE Thanks
lil
Guest
 
Posts: n/a

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.
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

Oh thanks but how do I send this to my professor who will read it in bloodshed? is this still c programing?
QUOTE Thanks
lil
Guest
 
Posts: n/a

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:

Vicky
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

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?
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

Like I would like to store the files in the project folder (the folder xcode creates when you create a project)
QUOTE Thanks
lil
Guest
 
Posts: n/a

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!

Vicky
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

^ ^
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

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);


main()

{
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");
exit(0);
}



while (fscanf(timbers,"%i",&id) != EOF )
{
fscanf(timbers,"%f",&maximum_load);
fscanf(timbers,"%f",&length);
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++)
{
do
{

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);
/*else
/* 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);
}


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




fclose(timbout);
fclose(timbers);
system("pause");
}//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));
if(maximum_load<=(.30)*(E)*(area)/(((length)/(width))*((length)/(width))))
{t_rue=0;
} //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;
}
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

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.
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

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.
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

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!

Vicky
Ok SHOUT! but what would that do?

Im so confused..
QUOTE Thanks
lil
Guest
 
Posts: n/a

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

ls

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)

~/Documents/Source/helloworld

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

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

Vicky
QUOTE Thanks
Sloopy
Guest
 
Posts: n/a

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?
QUOTE Thanks

Post Reply New Thread Subscribe


« Getting XDarwin to run | Matlab!, NO CD, and x11 »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
List view vs. Icon view - can't see all files =^o.o^= Switcher Hangout 8 01-20-2007 06:39 PM
Problems copying files... PC to Mac... ps zburd Running Windows (or anything else) on your Mac 0 10-10-2005 02:10 PM
repair permissions a waste of time? Macman Schweb's Lounge 5 05-31-2005 06:26 PM
opening ISO/HFS hybrid files in mac listenmusiq OS X - Operating System 2 04-29-2005 03:26 PM
Help me with auto renaming files in Finder Strider OS X - Apps and Games 4 02-03-2005 10:33 AM

All times are GMT -4. The time now is 08:50 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?