NO

Author Topic: Old Dog New tricks  (Read 30556 times)

xsilvergs

  • Guest
Re: Old Dog New tricks
« Reply #30 on: April 11, 2007, 10:06:32 AM »
I'm back again with what should be very simple.

I'm adding a couple of integars together and want to show them in a message box with some text.

I would like a message box  to show something like this, Your answer is 5. The 5 being the total of the addition.

Is it like this, do I have to convert the int to a string and how do I include it?

      WCHAR wszBuffer[40];
      swprintf(wszBuffer, L"%i", total);
      MessageBox(hwndDlg, L"Your answer is ", L"Addition result", MB_OK|MB_SETFOREGROUND);

Thanks for any help

Tony

JohnF

  • Guest
Re: Old Dog New tricks
« Reply #31 on: April 11, 2007, 10:14:05 AM »
WCHAR wszBuffer[40];
swprintf(wszBuffer, 39, L"%i", total);
MessageBox(hwndDlg, wszBuffer, L"Your answer is ", MB_OK|MB_SETFOREGROUND);

John


xsilvergs

  • Guest
Re: Old Dog New tricks
« Reply #32 on: April 11, 2007, 10:21:14 AM »
Hi John

What I was trying to do have a line of text in the message box that was made up of "Your answer is 5". Is it possible to join text (Your answer is) and the wszBuffer contents together? Something like this, but it doesn't work.

MessageBox(hwndDlg, L"Text" & wszBuffer, L"Addition result", MB_OK|MB_SETFOREGROUND);
« Last Edit: April 11, 2007, 11:00:09 AM by xsilvergs »

xsilvergs

  • Guest
Re: Old Dog New tricks
« Reply #33 on: April 11, 2007, 11:17:15 AM »
I'm still trying but can't combine L"Some text" with wszBuffer into one line.

A little more help would be apreciated.

JohnF

  • Guest
Re: Old Dog New tricks
« Reply #34 on: April 11, 2007, 11:33:52 AM »
WCHAR wszBuffer[40];
swprintf(wszBuffer, 39, L"Your answer is %i", total);
MessageBox(hwndDlg, wszBuffer, L"Title", MB_OK|MB_SETFOREGROUND);

John

xsilvergs

  • Guest
Re: Old Dog New tricks
« Reply #35 on: April 11, 2007, 11:37:15 AM »
Thank you John.

I had just worked it out myself, that's only taken me 3 hours but we got there in the end.

xsilvergs

  • Guest
Re: Old Dog New tricks
« Reply #36 on: April 11, 2007, 01:26:06 PM »
My next question is regarding joining of strings (concatenation).

I've declared str1, str2 and str3 as char, I would like to show the numbers as a string in a messagebox, here is the code I'm trying to use.

case BTN_ANS:
   {
      str1 = "1";
      str2 = "2";
      str3 = (char *)calloc(strlen(str1)  + strlen(str2) + 1, sizeof(char));
      WCHAR wszBuffer[40];
      swprintf(wszBuffer, L"Numbers are ", L"%s", str3);

MessageBox(hwndDlg, wszBuffer, L"My string is", MB_OK|MB_SETFOREGROUND);

Am I going about this in the wrong way and how should I do it?

Thank you

JohnF

  • Guest
Re: Old Dog New tricks
« Reply #37 on: April 11, 2007, 02:22:29 PM »
WCHAR str1[] = L"1";
WCHAR str2[] = L"2";
WCHAR wszBuffer[40];
swprintf(wszBuffer, 39, L"Numbers are %ls and %ls", str1, str2);
MessageBox(0, wszBuffer, L"My string is", MB_OK|MB_SETFOREGROUND);

You should read up on these things, no one is going to answer these simple questions on an ongoing basis.

THIS:
swprintf(wszBuffer, L"Numbers are ", L"%s", str3);

It is the second time you wrote swprintf that way, even after I showed the right way. Try and see what's wrong with it.

John
« Last Edit: April 11, 2007, 02:26:39 PM by JohnF »

JohnF

  • Guest
Re: Old Dog New tricks
« Reply #38 on: April 11, 2007, 02:54:12 PM »
Also, one cannot assign characters this way

str1 = "1";

in C that is a no-no.

If the string has room you can copy with this

wcscpy(wchar_t * restrict targetstring, const wchar_t * restrict sourcestring);

Which would be

   WCHAR str1[8]; // 8 is large enough
   wcscpy(str1, L"1");

Or create the string like this

WCHAR str1[] = L"1";

To concatenate

WCHAR str1[] = L"1";
WCHAR str2[] = L"2";
WCHAR * str3; // note the *, str3 is a pointer to an array of WCHAR, but at this point it points nowhere.

With this next line we allocate memory and assign the address to str3. The extra sizeof(WCHAR) is for the double termination of the string
str3 = malloc(wcslen(str1) + wcslen(str1) + sizeof(WCHAR));

strlen() is no good for WideChar strings

Now do it
swprintf(str3, wcslen(str1) + wcslen(str1) + sizeof(WCHAR), L"%ls %ls", str1, str2);

MessageBox(0, str3, L"My string is", MB_OK|MB_SETFOREGROUND);

John

xsilvergs

  • Guest
Re: Old Dog New tricks
« Reply #39 on: April 11, 2007, 03:04:17 PM »
John

Sorry if my questions seem simple but certain examples from the internet etc. don't seem to work. For example the:

swprintf(wszBuffer, 39, L"Numbers are %ls and %ls", str1, str2); won't work for me in pocket pc.

If I write it:

swprintf(wszBuffer, L"Numbers are %ls and %ls", str1, str2); it works fine.

There is probably an explanation for this but I haven't found it yet?

Thanks again for your help

Ps. I'll try your example you've just posted.

Tony

JohnF

  • Guest
Re: Old Dog New tricks
« Reply #40 on: April 11, 2007, 04:18:13 PM »
Ok, maybe it is different on WINCE.

Have you read the C FAQ? Might help with general stuff.

http://www.faqs.org/faqs/C-faq/faq/

John

JohnF

  • Guest
Re: Old Dog New tricks
« Reply #41 on: April 11, 2007, 04:43:38 PM »
Tony,

There is a mistake in my code when calculating the string length, you need to double for UNICODE, maybe best to create another variable.

WCHAR * str3;
int strLenght = ((wcslen(str1) + wcslen(str1)) * 2) + sizeof(WCHAR);
str3 = malloc(strLength);

Also you should free the memory when it is no longer required.

free(str3);

John
« Last Edit: April 11, 2007, 04:48:40 PM by JohnF »

xsilvergs

  • Guest
Re: Old Dog New tricks
« Reply #42 on: April 23, 2007, 02:26:04 PM »
Can somebody give me a clue please with my PocketPC problem?

I've now written some code to convert, sort and add, a text document containing ascii characters but I need to save the file with a new name.

The new name for the file is written in an Editbox. I then use GetDlgItemText to retieve the file name, this is in the format LPWSTR.

I am then trying to use f=fopen function with the name from the Editbox but of course it doesn't work because this want's a char *.

Could somebody explain how to convert please.

code used:
case BTN_SFA:
         GetDlgItemText(hwndDlg, TXT_FILE, wszBuffer, NELEMS(wszBuffer));
      
      f=fopen(????????????,"w");
      {
      for(d=0;d<=e;d++)
         fprintf(f,"%i,",data[d]);
      }
      fclose(f);

Thanks

Tony

sp00n

  • Guest
Re: Old Dog New tricks
« Reply #43 on: April 23, 2007, 03:00:30 PM »
Code: [Select]
char *filename;
wcstombs(filename, wszBuffer, wcslen(wszBuffer)+1);
f = fopen(filename, "w");
if it isn't work try this:
Code: [Select]
wchar wczBuffer[260];
... //your code
char filename[260];
wcstombs(filename, wszBuffer, 260);
f = fopen(filename, "w");


xsilvergs

  • Guest
Re: Old Dog New tricks
« Reply #44 on: April 23, 2007, 03:36:08 PM »
sp00n

Thank you. The char filename[260] works the other sample doesn't. I had tried using the wcstombs prior to writting but I couldn't get it to work. All I need to do is understand it now.

Thanks again

Tony