Re: LPCWSTR to CString conversion problem
I am trying out the following program...
////////////////////////////////////////////////////////////////////////////////////////////////
<config>
<key name="first" value="1250A"/>
<key name="second" value="1250B"/>
</config>
And I want to read a value of a key named "foo":
#include "stdafx.h"
#include <atlbase.h>
#include "xmllite.h"
#include <strsafe.h>
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
CComPtr<IStream> pFileStream;
CComPtr<IXmlReader> pReader;
XmlNodeType nodeType;
const WCHAR* pName;
const WCHAR* pValue;
//Open XML document
if (FAILED(hr = SHCreateStreamOnFile(L"config.xml",
STGM_READ, &pFileStream)))
{
wprintf(L"Error opening XML document, error %08.8lx", hr);
return -1;
}
if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader),
(void**)&pReader, NULL)))
{
wprintf(L"Error creating XmlReader, error %08.8lx", hr);
return -1;
}
if (FAILED(hr = pReader->SetInput(pFileStream)))
{
wprintf(L"Error setting input for XmlReader, error %08.8lx", hr);
return -1;
}
while (S_OK == (hr = pReader->Read(&nodeType)))
{
switch (nodeType)
{
case XmlNodeType_Element:
if (FAILED(hr = pReader->GetQualifiedName(&pName, NULL)))
{
wprintf(L"Error reading element name, error %08.8lx", hr);
return -1;
}
if (wcscmp(pName, L"key") == 0)
{
if (SUCCEEDED(hr =
pReader->MoveToAttributeByName(L"name", NULL)))
{
if (FAILED(hr = pReader->GetValue(&pValue, NULL)))
{
wprintf(L"Error reading attribute value, error %08.8lx", hr);
return -1;
}
if (wcscmp(pValue, L"foo") == 0)
{
//That's an element we are looking for
if (FAILED(hr =
pReader->MoveToAttributeByName(L"value", NULL)))
{
wprintf(L"Error reading attribute \"value\", error %08.8lx",
hr);
return -1;
}
if (FAILED(hr = pReader->GetValue(&pValue, NULL)))
{
wprintf(L"Error reading attribute value, error %08.8lx", hr);
return -1;
}
wprintf(L"Key \"foo\"'s value is \"%s\" ", pValue);
}
}
}
break;
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
I am using the above program with some modifications.
if (FAILED(hr = pReader->GetValue(&pValue, NULL)))
here pValue has the value "1280.50A." and i assign it to a cstring NewV.
There are no errors.
Then i use the following piece f code
for(i=0;i<NewV.GetLength();i++)
{
if(isdigit(NewV.GetAt(i)) == FALSE)
{
hasAlpha = TRUE;
}
}
But when i is 0, then the if loop gets executed where it should have not
entered. I hope you got where the problem is. Using iswdigit doesnt help
either. My project is in unicode.
Thanks,
Rohit.
"SvenC" wrote:
Hi,
sorry i intended to write getat(0). My cstring variable NewVersion which
contains the return value of GetValue() function has the value
"1280.50.". I get the first character of the string and pass it to
isdigit(). But it returns false. I guess there is some problem with the
LPCWSTR to CString conversion. When i manually initialize the NewVersion
as NewVersion = "1280.50.", then the first character wen passed to
isdigit() returns true.
Please show your code.
Do you compile as Unicode or Ansi? If Unicode use iswdigit to test a single
character.
--
SvenC
"Igor Tandetnik" wrote:
"Rohit Kumar" <RohitKumar@discussions.microsoft.com> wrote in message
news:95D7082C-4540-41A2-B485-E866F903C030@microsoft.com
I am using xmllite methods for reading from an xml file inside my
interface method. There is a function IXmlReader::GetValue which
returns the value of the xml element. The return value is an LPCWSTR.
When i try assigning it to a CString and use the first element of
that cstring using getat(1) then it returns a garbage value.
GetAt(1) returns the second character of the string, not the first. Are
you sure the string is long enough? What exactly do you mean by "garbage
value"?
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925