Solved

Problem to the line

Posted on 2014-11-20
8
177 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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 33

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

839 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