File paths and fopen()

konan

Registered
Question...

I am writing a program (C++, Carbon, Codewarrior 8, OS X) and I am having difficulty understanding how to handle macintosh paths. The following code should generate a full file name that can be opened by the fopen() command (ansi C), but it does not work.

the first argument (argv[0]) is returning something like

/Volumes/ ...<path>... /MyProgramFile

extracting the path "/Volumes/ ...<path>... /" and adding a file name to the end of it fail when passed to a function like fopen()

e.g.

/Volumes/ ...<path>... /Data.txt


How can I adjust the path such that it is compatible with fopen() or other Ansi C file functions?

Any help would be most appreciated,
Konan

p.s. Here is the pseudo code

int main(int argc, char *argv[])
{
char *FileName = argv[0];

char Path[256];

// extract the path from the full file name
GetPathFromFileName(Path,FileName);

char DataFile[256];
strcpy(DataFile,Path);
strcat(DataFile,"Data.txt");

FILE *InFile = fopen(DataFile,"r");
if (!InFile)
printf("Error opening the file %s\n",DataFile);

... etc....

}
 
What is the output of your printf("Error opening the file ?

The exact reason why fopen() failed is given in errno.

#include &lt;string.h&gt;
#include &lt;errno.h&gt;
...
if (!InFile)
{
int l_errno = errno;
printf("unable to open '%s': %s (errno=%d)\n",
DataFile, strerror(l_errno), l_errno);
}

What does it output?
 
Back
Top