• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Path Problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Path Problem


  • Subject: Re: Path Problem
  • From: Chris Espinosa <email@hidden>
  • Date: Tue, 23 Nov 2004 22:06:19 -0800

On Nov 23, 2004, at 8:52 PM, Sharayu Puranik wrote:

I do have one problem with setting path of files

I m using file related functions like fopen, fclose, fread, fwrite in Xcode
Objective-C project


 the problem I m facing is ;

 say, I have to open one text file present on Macintosh HD volume
 then I write the following code

 filePath = @"Macintosh HD:file1.txt";
 filePtr = fopen(filePath,"r");

 when I execute this code, the project crashes
 but if I write

 filePath = @"file1.txt";
 filePtr = fopen(filePath,"r");

where file1.txt file is present in current project directories Build folder then code
works smoothly


so how should the path of files be written?
means how to code path for files which r not in current project directory??

There are several things wrong here.

1) This list is primarily for questions about using the development tools, not using Mac OS X APIs and frameworks. You should probably ask further questions on the carbon-dev, cocoa-dev, or unix-porting lists instead of here.

2) When posting a new question, you can just post to the list address; sending individual copies to other list members is unnecessary.

3) You seem to be mixing Objective-C (the @" " string constants), Carbon (colon file delimiters), and UNIX (fopen). While this is possible, it's unnecessary and not straightforward. It would probably help to pick one programming paradigm and stick with it:
- C or C++ and UNIX APIs for UNIX-style tools
- C or C++ and Carbon APIs for legacy Carbon code or Carbon end-user graphic user interface applications
- Objective-C and Cocoa frameworks for new Cocoa end-user GUI applications


4) File paths on Mac OS X should be slash-delimited, not colon-delimited. The root directory is "/" (even if the volume name is set to something like "Macintosh HD"). If you want to read a file at the top level of the boot disk, you should write

 filePath = "/file1.txt"
 fileRef = fopen(filePath, "r");

(Note that the file path is a conventional char * style C string, which is what fopen expects; the Objective-C @"/file1" notation creates a constant NSString object).

5) Performing operations on the "current working directory" is generally unreliable. In many cases in Mac OS X this is just plain not set at all, and in others it is extremely dependent on how your code is packaged and how the user invoked it. Don't write code that assumes that the working directory will be set to anything predictable. If you're writing a UNIX tool, use an environment variable or pass in the directory or complete path in an argv[] argument. If you're using Carbon, use the FSFindFolder() function to get a file reference (FSRef) to particular directories (e.g. user home directory, etc.). For Cocoa, most relevant functions have built-in defaults for the directory they operate on.

6) Similarly, hardcoding file names is a bad idea. Use argv[] arguments for Unix tools, or the Navigation Services API for Carbon, or NSOpenPanel for Cocoa. If you use the latter you'll probably end up using FSRefs and/or CFURLs to refer to files, instead of path strings, which is also good.

If you are using Objective-C, you probably want to read this documentation pretty thoroughly before proceeding:

http://developer.apple.com/documentation/Cocoa/Conceptual/ LowLevelFileMgmt/index.html

It should answer the questions you'll come up with when you get beyond those first two lines. As I said, further questions should probably go to the email@hidden (Unix questions), email@hidden (Objective-C and Cocoa questions), or email@hidden (Carbon API questions) lists.

Regards,

Chris

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Path Problem (From: "Sharayu Puranik" <email@hidden>)

  • Prev by Date: Path Problem
  • Next by Date: Re: APPLE GUYS: Do you want Shark profiles of XCode when it hangs?
  • Previous by thread: Path Problem
  • Next by thread: XCode editor preferences
  • Index(es):
    • Date
    • Thread