From ddjolley at gmail.com Sun Nov 20 12:11:03 2005 From: ddjolley at gmail.com (Doug Jolley) Date: Sun, 20 Nov 2005 12:11:03 -0800 Subject: [OCLUG-devel] fopen doesn't like my path Message-ID: 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. ... doug -------------- next part -------------- An HTML attachment was scrubbed... URL: http://localhost.localdomain/pipermail/oclug-devel/attachments/20051120/3ec68cff/attachment.html From michael.elkins at gmail.com Sun Nov 20 18:28:33 2005 From: michael.elkins at gmail.com (Michael Elkins) Date: Sun, 20 Nov 2005 18:28:33 -0800 Subject: [OCLUG-devel] fopen doesn't like my path In-Reply-To: References: Message-ID: <404f03220511201828x40b1d88bnc0a88776928dca7d@mail.gmail.com> You'll likely need to post the code snippet your using in order for us to determine what the exact issue is. Nothing in your description seems incorrect. Michael On 11/20/05, 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. > > ... doug > > _______________________________________________ > OCLUG-devel mailing list -- OCLUG-devel at oclug.org > http://mailman.oclug.org/mailman/listinfo/oclug-devel > > > From ddjolley at gmail.com Sun Nov 20 20:29:53 2005 From: ddjolley at gmail.com (Doug Jolley) Date: Sun, 20 Nov 2005 20:29:53 -0800 Subject: [OCLUG-devel] fopen doesn't like my path In-Reply-To: <404f03220511201828x40b1d88bnc0a88776928dca7d@mail.gmail.com> References: <404f03220511201828x40b1d88bnc0a88776928dca7d@mail.gmail.com> Message-ID: Hi, Michael. Thanks for responding. I got it figured out. It turns out (apparently) that when one extracts a token from a string using strtok a newline character is appended to the token. It was the trailing newline character in the path token that fopen didn't like (as it shouldn't). Once I chopped it off everything worked just fine. It did have me going for quite a while though. Thanks again for the response. ... doug -------------- next part -------------- An HTML attachment was scrubbed... URL: http://localhost.localdomain/pipermail/oclug-devel/attachments/20051120/bf1f5e7d/attachment.html From x at xman.org Sun Nov 20 21:28:59 2005 From: x at xman.org (Christopher Smith) Date: Sun, 20 Nov 2005 21:28:59 -0800 Subject: [OCLUG-devel] fopen doesn't like my path In-Reply-To: References: Message-ID: <43815B1B.8020401@xman.org> 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 From x at xman.org Sun Nov 20 21:34:44 2005 From: x at xman.org (Christopher Smith) Date: Sun, 20 Nov 2005 21:34:44 -0800 Subject: [OCLUG-devel] fopen doesn't like my path In-Reply-To: References: <404f03220511201828x40b1d88bnc0a88776928dca7d@mail.gmail.com> Message-ID: <43815C74.9020709@xman.org> Doug Jolley wrote: > Hi, Michael. Thanks for responding. I got it figured out. It turns > out (apparently) that when one extracts a token from a string using > strtok a newline character is appended to the token. It was the > trailing newline character in the path token that fopen didn't like > (as it shouldn't). Once I chopped it off everything worked just > fine. It did have me going for quite a while though. Hehe, just got your post and Michael's. ;-) strtok shouldn't be inserting a trailing newline anywhere. The only character it inserts is a NULL (which replaces the delimiter). Are you sure something else wasn't inserting the newline (or for that matter, the newline might have already been there)? --Chris From ddjolley at gmail.com Mon Nov 21 09:30:35 2005 From: ddjolley at gmail.com (Doug Jolley) Date: Mon, 21 Nov 2005 09:30:35 -0800 Subject: [OCLUG-devel] fopen doesn't like my path In-Reply-To: <43815C74.9020709@xman.org> References: <404f03220511201828x40b1d88bnc0a88776928dca7d@mail.gmail.com> <43815C74.9020709@xman.org> Message-ID: > > (or for that matter, > the newline might have already been there)? > Chris, I think that was probably the case. The string that strtok was parsing came from a data file. In this case the datafile was a text file with colon delimited fields and newline delimited records. Guess what? The particular field in question was the last field in the record therefore it was followed by a newline character. It occurs to me (now that I better understand this) that rather than chopping off the trailing newline character what I should have done as the fix was include a newline in the strtok delimiters. Thanks for pointing out the error of my ways because I probably would have gone away with the misconception that strtok appends a newline character and that would have probably caused me additional problems in the future. ... doug -------------- next part -------------- An HTML attachment was scrubbed... URL: http://localhost.localdomain/pipermail/oclug-devel/attachments/20051121/92b597ef/attachment.html