Using files with Xtool

S

Sloopy

Guest
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>
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
 
OP
L

lil

Guest
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
 
OP
L

lil

Guest
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.
 
OP
S

Sloopy

Guest
Oh thanks but how do I send this to my professor who will read it in bloodshed? is this still c programing?
 
OP
L

lil

Guest
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
 
OP
S

Sloopy

Guest
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?
 
OP
S

Sloopy

Guest
Like I would like to store the files in the project folder (the folder xcode creates when you create a project)
 
OP
L

lil

Guest
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
 
OP
S

Sloopy

Guest
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;
}
 
OP
S

Sloopy

Guest
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.
 
OP
S

Sloopy

Guest
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.
 
OP
S

Sloopy

Guest
lil said:
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..
 
OP
L

lil

Guest
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
 
OP
S

Sloopy

Guest
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?
 
OP
L

lil

Guest
Ok looked at your source.

That source file you want to call main.c

Now, pick somewhere on your hard disk where main.c will be stored. Lets say for continuity with my last post you choose to save main.c in a folder called source in your Documents folder.

The path to that folder is:

~/Documents/source

The tilde means your home folder. Otherwise you would need to do something like /Users/MyProfile/Documents/source which will work but is a long winded way of typing it.

So, if you want timbers.txt and timbout.txt to also end up in this Source folder with main.c these are the bits you would change:

--
timbers = fopen("~/Documents/source/timbers.txt", "r");
if (timbers == NULL)
{
printf("sorry there is no file \n");
system ("pause");
exit(0);
}
---

AND

--
//concludes true/else statement
timbout = fopen("~/Documents/source/timbout.txt", "w+");
--

Also, sure you do know but strictly speaking // isn't used for comments in C, the ANSI C standard is /* */ only, though some compilers these days accept // as used in C++ and ObjectiveC. You may already know this but some C compilers will baulk at // for commenting.

Vicky
 
OP
L

lil

Guest
Excuse me no need to shout, I am actually trying to help you here.

If you read my next post you store it with main.c.

The mac isn't being confusing - I think you need to be a bit clearer, you can save the files wherever you like, they are not being saved in random places, you are telling them where they are stored.

The Mac uses a UNIX alike file system structure - not Windows, they are different, and if you want todo this on the Mac it is easy enough, you have to appreciate the file system differences here.

You don't have to even use ~/ bla bla

You can just use "timbout.txt" and it will end up wherever the executable that was compiled from main.c was. I tend to use explicit paths in specific examples such as this in case someone doesn't realise what files need to be moved with the executable.

And I won't take shouting at for trying to help you here, I really don't think that is on.

Vicky
 
OP
L

lil

Guest
Suffice to say it works here, compiled first time and ran.

The files were in the same folder as main.c and the resulting executable.,

EDIT: noticed the system pause calls, not everyone will have a pause command (I just happen to have just that thing), and it may be why it fails for you on a Mac.

This was the result of timbout.txt FYI.

ID Length Load Width x Length
16745 120.0 9000.0 6.0 x 22.6
16789 240.4 20000.0 8.0 x 22.6
22334 60.0 20000.0 10.0 x 22.6
54321 200.2 16000.0 12.0 x 22.6
12345 150.5 25000.0 14.0 x 22.6
65123 40.0 10000.0 16.0 x 22.6
77777 22.6 1000.0 18.0 x 22.6

Vicky
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top