0

I have a value in character pointer variable and the value is changed dynamically.

for e.g

one case: char *ptr = sometext || abc.txt; second case: char *ptr = abc.txt || sometext; 

I need only file name in another variable. str = abc.txt

currently I am using the code:

CString str; char* lpszToToken = stLine.GetBuffer(stLine.GetLength()); char* lpszToken = strtok(lpszToToken, "|"); while(lpszToken != NULL) { lpszToken = strtok(NULL, "|"); str = lpszToken; } 

but it working in only first case: I need a genric code to run in both case Any idea? Thanks,

2 Answers 2

0

My C is rusty, but why not something like:

char* fname = strtok( fileNames, "|"); while (fname != NULL) { // do something with the file here.. processFile( fname); fname = strtok( NULL, "|"); // next. } 

Also, Hungarian notation is a horrible way to program. It effectively conceals & obfuscates most meaning, rendering clear and simple problems opaque & ugly. See how readable my example is, compared with the gack you supplied?

Your instructor (and Petzold) were very unfortunately mistaken to be teaching this to you. I suggest writing "literate" code instead.

See:

Sign up to request clarification or add additional context in comments.

3 Comments

@sasha.sochka Please provide a rationale/link for this kind of general statements.
If my opinion was somewhere counted I would vote that strtok is the worst designed function for ever (in C)
I agree with Sasha. Hidden global state (worse than using global variables, since it is opaque), unclear API design & security issues. stackoverflow.com/questions/5999418/… But hey, I used it when I was a newbie too.
0

Since you have CString available you can use CString::Find or CString::Tokenize

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.