Solved

Problem to the line

Posted on 2014-11-20
8
176 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use of condition with 'serial' in ansible 2 59
mapBully challenge 6 133
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 135
Making an alias 7 95
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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