Solved

Problem to the line

Posted on 2014-11-20
8
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 11

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
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!

 
LVL 11

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 11

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

695 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