LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with strtok and a string with '\\' at it' end

Solved!
Go to solution

I faced a problem with "strtok" as I programmed a function which checked a directory (string) on existance and if it does not exist it would create that directory. That problem did only occur if in that "strtok"-broken string is a '\\' at the end of the string.

 

Example: Directory-string is "E:\\xyz\\uvw\\abc\\qwertz\\bnm\\".

The function tests if "E:\\xyz" exists and if not it creates it.

The function tests if "E:\\xyz\\uvw" exists and if not it creates it.

 ... and so on...

The function tests if"E:\\xyz\\uvw\\abc\\qwertz\\bnm" exists and if not it creates it.

 

After using that function with directory-string "E:\\xyz\\uvw\\abc\\qwertz\\bnm\\" I use an other function with that directory-string "E:\\xyz\\uvw\\abc\\qwertz\\bnm\\" as a parameter (creating a file inside - a text-log file!). But now in that second function (in debug-mode!) the directory-string "E:\\xyz\\uvw\\abc\\qwertz\\bnm\\" is only "E" - nothing more! The rest is missing!

BUT if I use the directory-string "E:\\xyz\\uvw\\abc\\qwertz\\bnm" instead the directory-string "E:\\xyz\\uvw\\abc\\qwertz\\bnm\\" in the next function is "E:\\xyz\\uvw\\abc\\qwertz\\bnm\\" !!!!

 

It is so that "strtok" results an error inside the compiler which influences the string-parameter of a following function.

Maybe it influences in general the following string but I didn't test that.

 

My solution was:

I check the string in the first function on '\\' at it's end. If there is '\\' I cut it away - as long as there is a '\\' and the string is longer than 1 !

 

 

0 Kudos
Message 1 of 4
(4,098 Views)

Hello Frankie!

 

I think that the problem you might be experiencing is that strtok actually modifies the source argument string. This might explain why your directory string only contains "E", after strtok modified the string.

You might want to try to debug the code example from http://www.cplusplus.com/reference/cstring/strtok/ as a reference, to understand how strtok handles its string arguments.

 

As a workaround, you could pass a temporary copy of your original directory string to strtok, such that your initial string remains unchanged.

 

I hope this helps!

0 Kudos
Message 2 of 4
(4,066 Views)
Solution
Accepted by topic author FrankieBoy98

Thank you for your post "Johannes_T".

 

For my purposes it is enough to check the string on '\\' at it's end and if there is a '\\', I cut it away and continue with that "cutting" as long as there is a '\\' at it's end and the string is longer than 1.

 

Best Regards,

F.

 

0 Kudos
Message 3 of 4
(3,782 Views)

Yeah, it's better to make a copy of the string before messing with strtok. But more generally if you are manipulating directory names, why don't you use MakePathname() and SplitPath() ? It'll even work on filesystems that use '/' instead of '\'.

0 Kudos
Message 4 of 4
(3,744 Views)