Solved

Problem to the line

Posted on 2014-11-20
8
179 Views
Last Modified: 2014-11-21
Hi,
How to correct the last line below?

struct nameval
{
	char fld_nm[100];
	wchar_t fld_nm_t[100];
	int fld_len;
	int fld_val;
};
nameval binrec;

...

int main()
{
    std::vector<nameval> records;
    int cnt;
	for (cnt=0;cnt<20000;cnt++)
	{
		nameval val={0};
		...
		val.fld_nm_t=val.fld_nm;
		...

Open in new window

due to this error?
1>  stdafx.cpp
1>  pro_b.cpp
1>c:\pro_b\pro_b\pro_b.cpp(64): error C2440: '=' : cannot convert from 'char [100]' to 'wchar_t [100]'
1>          There is no context in which this conversion is possible

Open in new window

0
Comment
Question by:HuaMinChen
  • 4
  • 3
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40456762
You are wanting to convert a Unicode to ansi.
See:
http://msdn.microsoft.com/en-us/library/8tdk45ff.aspx
for the wctomb function which will do this for you.  There is example code there.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40456861
How to correct the last line below
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <string>
#include <ctype.h>
#include <time.h>
#include <process.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
struct nameval
{
	char fld_nm[100];
	wchar_t fld_nm_t[100]; 
	int fld_len;
	int fld_val;
};
nameval binrec;

int main()
{
    std::vector<nameval> records;
    int cnt;
	for (cnt=0;cnt<20000;cnt++)
	{
		nameval val={0};
		int j;
		for (j=0;j<20;j++)
		{
			val.fld_nm[j] += (char)(rand () % 58 + 64);
		}
		
		wchar_t wbuf[100]={0};
		mbstowcs(wbuf,val.fld_nm,_countof(val.fld_nm));
		val.fld_nm_t=wbuf;
		...

Open in new window

due to this error?
1>------ Rebuild All started: Project: pro_b, Configuration: Debug Win32 ------
1>  stdafx.cpp
1>  pro_b.cpp
1>c:\pro_b\pro_b\pro_b.cpp(64): warning C4996: 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdlib.h(498) : see declaration of 'mbstowcs'
1>c:\pro_b\pro_b\pro_b.cpp(65): error C2106: '=' : left operand must be l-value

Open in new window

0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 390 total points
ID: 40456869
You could try to copy the contents of one buffer to the other, eg. wmemcpy:
http://msdn.microsoft.com/en-us/library/dswaw1wk.aspx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:HuaMinChen
ID: 40456909
can I have more details to use memcpy? thanks
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40456985
What about the example given isn't clear - it involves one line of code.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40456999
I am not sure about its syntax. sorry
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40457009
Is this what you need?
wmemcpy(val.fld_nm_t, wbuf, 100);
0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 110 total points
ID: 40457447
in c++ memcpy is not suitable to copy strings. you better use wstring and string or bstr_t tclass which can handle both char type. extra copy is not necessary here.

val.fld_nm_t=val.fld_nm;
the above doesn't compile for two reasons. first char and wchar_t have different size and it is a c statement where arrays could not be assigned to each other. but the wcstombs (or wcstombs_s) are fine to convert and copy from one to the other:

size_t ret  = 0;
wcstombs_s(&ret , val.fld_nm, sizeof(val_fld_nm), val.fld_nm_t, wcslen(val.fld_nm_t));

Open in new window



Sara
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question