Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sscanf problem/misconception

Status
Not open for further replies.

base2

Programmer
Apr 18, 2002
7
US
I am trying to use sscanf as follows:

CString word1, word2, word3;
CString word_string = "three word string";
sscanf(word_string, "%s%s%s", word1, word2, word3);
AfxMessageBox(word1);

I would expect this to display a dialog box containing "three", is that a correct understanding of what this *should* do?

It actually displays a dialog box containing "string". Further, I have verified that the CStrings word1, word2 and word3 all contain "string". The function returns a value of 3 as I would expect, indicating that it assigned values to three arguments.

I have checked out the CString class and don't see an equivalent method for doing this sort of thing. I saw the Format method that parallels sprintf but nothing like sscanf.

Is there any known bug here or is this a Unicode/MBCS issue or do I just have a misconception about syntax or function?

Any help would be appreciated.
 
Could someone please just confirm or refute my understanding of this library function, thanks.
 
In your program , word1, word2,word3 are all uninitialized. Somehow their internal buffer point to the same address, when they are not initialized. sscanf needs the memory address for its arguements. It looks like the compiler interpretes CString as its buffer address. That is why you get the same string for the three different variables.

try initialize them like,
CString word1 = "A",word2 = "b", word3 = "c". this will work.

But I suggest you use char array for the sscanf instead of CString because you know its behaviour explicitly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top