Newbie Question about using selected file name with fopen
Newbie Question about using selected file name with fopen
- Subject: Newbie Question about using selected file name with fopen
- From: Phil Faber <email@hidden>
- Date: Fri, 7 Apr 2006 23:44:57 +0100
Hello all. I'm sure this is a Newbie question but as a newbie, I'm
probably OK to ask it!
I'm trying to prompt the user to select a file, and then to open the
selected file and read in one byte from it.
(Actually I'm trying to achieve a lot more that that pointless task
but have removed most of the other stuff for the purposes of this
question!)
I'm using the following code:
char oneByte;
// Provide dialogue box to select file
NSOpenPanel *panel = [NSOpenPanel openPanel];
// Put file name into myFile
NSString *myFile = [panel filename];
// Display file name in textfield (this works fine)
[statusText setStringValue:myFile];
// Prepare file pointer to open selected file
FILE *fp1;
// Set file pointer to selected file (this line fails)
fp1=fopen(myFile,"rb");
// Read a byte
fread(&oneByte,1,1,fp1);
// Close the file
fclose(fp1);
Everything works fine except the <fp1=fopen(myFile,"rb");> line. The
user is prompted for the file and the file name selected does end up
on myFile. (I know this because the <[statusText
setStringValue:myFile];> line successfully displays the chosen file
name in the <statusText> text field.
However, the <fp1=fopen(myFile,"rb");> fails to set the file pointer
to that file, giving me a <warning: passing argument 1 of 'fopen'
from incompatible pointer type> error. If I replace it with
<fp1=fopen("FileName.txt","rb");> then the code builds perfectly but
obviously will only ever open the file <FileName.txt> which defeats
the object!
As I already know that myFile DOES contain the file name (including
path), what should I be changing <fp1=fopen(myFile,"rb");]> to make
it work?
Any help would be greatly appreciated.
Phil
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden