Problems with CString and fscanf
-
Why does this code work?: char test[12], test1[12]; ....more code... fscanf(fp,"%d %s %s %s %s",&hour,station,country,test,test1); And this doesn`t: CString test, test1; ....more code... fscanf(fp,"%d %s %s %s %s",&hour,station,country,test,test1); The CString values are correct just after the 'fscanf', but they disappear after some other function calls.
-
Why does this code work?: char test[12], test1[12]; ....more code... fscanf(fp,"%d %s %s %s %s",&hour,station,country,test,test1); And this doesn`t: CString test, test1; ....more code... fscanf(fp,"%d %s %s %s %s",&hour,station,country,test,test1); The CString values are correct just after the 'fscanf', but they disappear after some other function calls.
You are directly modifying the contents of the CString without locking the buffer first.
CString test, test1;
LPTSTR pszBuff, pszBuff1;pszBuff = test.GetBuffer(12);
pszBuff1 = test1.GetBuffer(12);Now you can use pszBuff/pszBuff1 in fscanf(). Call ReleaseBuffer() on both CStrings once you're done directly modifying the contents. --Mike-- My really out-of-date homepage "Why does anyone have a web page? Too much free time... not enough friends... justifying owning a computer." -- Noel Crane on Felicity Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
-
You are directly modifying the contents of the CString without locking the buffer first.
CString test, test1;
LPTSTR pszBuff, pszBuff1;pszBuff = test.GetBuffer(12);
pszBuff1 = test1.GetBuffer(12);Now you can use pszBuff/pszBuff1 in fscanf(). Call ReleaseBuffer() on both CStrings once you're done directly modifying the contents. --Mike-- My really out-of-date homepage "Why does anyone have a web page? Too much free time... not enough friends... justifying owning a computer." -- Noel Crane on Felicity Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
Thank you, so much. And then if I modify the values in many places, this way: if (...) test="some word", and so on... What must I do? Have I to make a GetBuffer before modifying it and ReleaseBuffer after each one?
-
Thank you, so much. And then if I modify the values in many places, this way: if (...) test="some word", and so on... What must I do? Have I to make a GetBuffer before modifying it and ReleaseBuffer after each one?
José Luis Sogorb wrote: Have I to make a GetBuffer before modifying it and ReleaseBuffer after each one? Yep. See the docs on CString::GetBuffer() for some examples. --Mike-- My really out-of-date homepage "Why does anyone have a web page? Too much free time... not enough friends... justifying owning a computer." -- Noel Crane on Felicity Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.