Removing substring from string before calling strtok in C -


I have a variable that I would like to divide into an array of paths:

  PATH = / bin: / usr / bin: / usr / local / bin   

The result of the above string would be:

  [0] -> ; / Bin [1] - & gt; / Usr / bin [2] - & gt; / Usr / local / bin   

If I call strtok on the string with only the delimiter : then I get the result:

  [0] - & gt; Path = / bin [1] - & gt; / Usr / bin [2] - & gt; / Usr / local / bin   

But still I have a substrings in the index before the PATH = array. I need to find a way to remove the string from PATH = before calling on strtok .

Rather than relocating a new array of four instead of PATH = substring, I thought I had the four signals after PATH = I can increase the first letter to indicate.

  four prefix [] = "path ="; If (strstr (str, prefix)) {str + = strlen (prefix); }   

So now the indicator SR points before / after PATH = . Line str + = after strlen (prefix) :

  path = / bin: / usr / bin: / usr / local / bin a ????   

and I get the following array of paths from strtok .

  [0] - & gt; / Bin [1] - & gt; / Usr / bin [2] - & gt; / Usr / local / bin   

Will this be considered bad behavior in C? Is there any side effects from doing this? Do I want another approach: I. E. Assign a new buffer and copy the value of the str in the new buffer without PATH = .

call strtok () with "=" first call the delimiter, then call with "code:": ") Then you can change the delimiter on each call to strtok () if it does need to.

Of course, it assumes that you strtok) travels your string in the first place. Make sure you are working on a copy of your PATH variable.

The offer you offer also works fine too. I saw that the only hold is, if str is the only indicator to start the dynamically assigned space, you can not free up space (memory leak) for a long time. Do you need to decide that this is a problem? And if so, the solution is simple: keep a copy of the pointer which needs to be free so that you can clear it.

Also keep in mind that PATH is really weird:

  pat =: / usr / bin :: / bin:   

Three 'inherent' . elements are: first : , middle of the middle, and after the last one. Obviously, you usually do not have three at a time, but you need to know the rules of the game.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -