Creation of file using programming????

Discussion in 'Mac Programming' started by naveen, Jun 23, 2005.

  1. naveen macrumors member

    Oct 27, 2004
    hi all
    I want to create a file(that may be .wav,AIFF,rtf or other) on mac system through programming. How i can do this.

    Thanks and regards
  2. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
    Your question is very vague. What language would you be using? The answer would be different depending upon what you'd be using.

    With Cocoa actually saving the file could be just one line of code but you'd need to prepare what you wanted to save in (probably) an NSData object first. Some file types have their own special classes to deal with this sort of thing.
  3. weg macrumors 6502a


    Mar 29, 2004
    Open a Text editor, write:


    echo "My Content" > filename.extension

    Ok, just joking..
  4. GeeYouEye macrumors 68000


    Dec 9, 2001
    State of Denial
    You go out and buy a book on Cocoa Programming (Aaron Hillegass wrote a good one). You read said book and follow along. Your answers will be in there for RTF, at least. For the others, read their specs and make an implementation of it, relatively easy in C.
  5. naveen thread starter macrumors member

    Oct 27, 2004
    Hi all

    I want to create these files using c,c++ on macintosh system. how can i save these files. how the resource fork will be handled . Plz help me i am new to mac os.

    Thanks and regards
  6. mkrishnan Moderator emeritus


    Jan 9, 2004
    Grand Rapids, MI, USA
    Correct me if I'm wrong, but cross-platform files like the ones you described don't use resource forks, do they? Like the others said, you'd use Cocoa APIs to generate the files.

    Here is some stuff from Apple's support pages:
  7. naveen thread starter macrumors member

    Oct 27, 2004
    hi all
    I am new to Mac Os. I want to create the file using C/C++ programming.

    I create the file using following code and the file created but i unable to open this file.This is a .AIFF file.
    FILE *fp;
    fp = fopen(Audio 1_01,"wb+");


    DataBuff buffer contains the raw contents of file.

    I have also created the resource fork file using
    File fp;
    fp = fopen(._Audio 1_01,"wb+");
    ResourceBuff buffer contains the resource raw data of file.

    When i open the created file using the hex editor then I see File Type/Creator
    is empty.

    But i don't know how to deal with this resource fork file creation. How the created resource fork file linked to its data fork file.

    When i open the created file using the hex editor then I see File Type/Creator
    is empty.

    If any body have idea then plz help me.

    Thanx in advance
  8. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
    OK, we getting nearer. So you want to use C or C++ - well you could use either the BSD API's to do your file handling or Carbon. I know next to zero about carbon except that I've seen enough to want to avoid it. The BSD API's are similar but sometimes a necessary evil.

    As I use the Cocoa frameworks whenever I can, I have to admit I don't really think too much about resource forks. My understanding is that they're kind of in a no-mans land with Apple varying between being ambivalent and deprecating their use. Having said that, Apple upgraded the Unix command line tools in Tiger so they now (finally) now what a resource fork is.

    Getting back to the main point of your question I think actually saving your file is the least of your worries. You should be more worried about how you're actually going to create the data structures you want to save. You haven't said if you already have that part done and just want to know how to save, or if you really want to know how to create those data structures - which is a much much harder question.

    Edit: Just seen your new post. The resource fork is (I believe) stored as a separate file. Do you actually need the resource fork? Does the file you create 'work' without it? Check out this link
  9. BornAgainMac macrumors 603


    Feb 4, 2004
    Florida Resident

    Or just type in

    touch filename.extension
  10. jcgerm macrumors member

    May 28, 2003
    Honestly, I wouldn't try to deal with resource forks and data forks. What you need is an API that can read/write these file types, so that you don't have to deal with them (i.e. ImageMagick for jpeg, tiff, etc.). Unless you want to sift through file specifications, I would look for an API that does all of this for you. No sense in reinventing the wheel here.
  11. whooleytoo macrumors 604


    Aug 2, 2002
    Cork, Ireland.
    For Wav or AIFF files, take a look at the QuickTime API, it should be able to export files of this type easily.
  12. naveen thread starter macrumors member

    Oct 27, 2004
    As I earlier told that i am a new programmer on mac. I have knowledge of Mac(HFS and HFS+) file system. I have written a small programe in my pc(using visual c++) to recover files of a crashed mac HDD is connected to pc then my program is able to recover both data fork and resource fork. If we transfer both data fork and rosource fork of a file to mac machine the file run perfectly.

    Now i am writing the same program on mac machine. I know the location of both data fork and resource fork . But my problem is how to associate them with each other. I have already discuss how i am writting both the files(data fork and resource fork).

    if any body have some idea then plz help me

    Thanks and regards
  13. MarkHamilton macrumors newbie

    Jun 21, 2005
    Albuquerque, NM, USA

    The 'Resource Manager' reference is probably where you want to look to find the information you need.

    In particular, check out this function:
  14. naveen thread starter macrumors member

    Oct 27, 2004
    Hi all

    I have using the FSpCreate() for creating a file, when i compiling my application there is no error but still file is not created.I want to know about the FSSpec structure.

    I have passed the following values to this structure
    unsigned char filename[64]="Aman";;
    FSpCreate(&spec, 'ttxt', 'TEXT', smSystemScript); *

    I want to create file on root . What i should pass to parId and vRefNum?? How i can get the Volume reference number.

    Thanks and Regards

  15. boomtopper macrumors member


    Mar 25, 2004
    UK Huddersfield
    Ok to do reading and writing of audio files it can be quite complex. Especially when you are manipulating the data using DSP. But here are some useful links i can give you to help.

    1. The first way is to read about how wav files are structured. Becuase wav files have information burried in them such as what sample rate and bit depth. Using this source you can learn how a wav file is structured and then create your own api to read and write them.

    2. You can also get this open source api called libsnd. This api will alow you to read and write wavs and other file formats.I would recommend using this becuase it works on all platforms. I would still recommend getting familiar with the wav file format, and make sure you know what all the terminology means such as sample rate.

    I hope this helps.
  16. logicat2001 macrumors regular

    Apr 16, 2003
    Minneapolis, MN
    You might also read up on OS X's Audio API's re: file format conversion here: file:///Developer/ADC%20Reference%20Library/documentation/MusicAudio/Reference/CoreAudio/audio_toolbox/chapter_4_section_1.html#//apple_ref/doc/uid/TP30001108-CH205

    I hope the URL translates properly, but if not, if you've installed the latest Developer docs, you should search for AudioToolbox. There are a number of convenience methods that allow for reads/writes of common audio file formats. It may or may not fit your needs, but FYI.


Share This Page