Solved

Problem to the line

Posted on 2014-11-20
8
169 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
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40456909
can I have more details to use memcpy? thanks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 32

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now