[OCLUG-devel] fopen doesn't like my path

Christopher Smith x at xman.org
Sun Nov 20 21:28:59 PST 2005


Doug Jolley wrote:

> I am using fopen to open two different files.  In the first case 
> (which works just fine), I simply define the path to the file as 'char 
> path2file[]="/path/to/file"'.  I then use path2file as the first 
> argument to fopen.  In the second case (which doesn't work so well), I 
> extract the path to the file from colon-delimited data contained in a 
> string using strtok.  IOW, I use the  following 
> 'path=strtok(NULL,":")'.  I use NULL here because I have previously 
> extracted other tokens and the path is the next token to be 
> extracted.  I inserted a printf statement to print 'path' and it does 
> in fact point to the correct data.  However, when I put 'path' as the 
> first argument to fopen, I get a segmentation fault.  If I replace the 
> 'path' argument to fopen with the actual correct path to the file (by 
> just including the actual string enclosed in double quotes), it works 
> just fine.  So, it certainly appears that fopen doesn't like my path 
> pointer which was derived from strtok; but, I have absolutely no idea 
> why.  Can anyone help me out?  Thanks for any input.

At first glance, it sounds like it *should* be working. Perhaps you 
could post a code sample that demonstrates your problem? I find it a 
helpful excercise to come up with a code sample for these kinds of 
problens, because often the problem goes away after you've removed what 
you think are irrelevant bits of code. If the problem is still there, 
then you have a concise way of describing it.

--Chris


More information about the OCLUG-devel mailing list