473,608 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sscanf_s cannot convert ...from 'WCHAR[260] to 'const char *' error.

37 New Member
Hello,

I have a small program which uses FindFirstFile() to get a file name. Once I get the file name, I would like to use sscanf to extract certain part of the file name.

My program is as follows :

......
int _tmain(int argc, _TCHAR* argv[])
{
WIN32_FIND_DATA ffd;
TCHAR szDir[MAX_PATH];
HANDLE hFind = INVALID_HANDLE_ VALUE;
DWORD dwError = 0;
char filename[128];
char ext[128];

StringCchCopy (szDir, MAX_PATH, TEXT(".\\*.dat" ));
hFind = FindFirstFile (szDir, &ffd);
if (hFind == INVALID_HANDLE_ VALUE)
{
dwError = GetLastError();
if (dwError == ERROR_PATH_NOT_ FOUND)
_tprintf (TEXT("[Error] Directory not found\n");
else
_tprintf (TEXT("FindFirs tFile failed (%u)\n", dwError);
return dwError;
}
else
{
_tprintf(TEXT ("File name : %s\n"), ffd.cFileName);
if (sscanf_s (ffd.cFileName, "%16s.%3s", filename, ext) == 2)
{
printf("File Name : %s\n", filename);
printf("Extensi on : %s\n", ext);
}
else
printf ("sscanf_s failed\n");
}
}

And I got a compile-time error message saying "error C2664 : 'sscanf_s' cannot convert parameter 1 from 'WCHAR [260]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cas t, C-stype cast or function-style cast.

Could anyone please help me fix this compile-time error?

Thank you very much for your help.

Akino
Jun 30 '08 #1
2 13149
gpraghuram
1,275 Recognized Expert Top Contributor
Hi,
The variable cFileName is of type TCHAR.
This is what i see in the TCHAR definition when searching in MSDN.
Expand|Select|Wrap|Line Numbers
  1. #ifdef Unicode
  2. typedef WCHAR TCHAR;
  3. #else
  4. typedef CHAR TCHAR;
  5. #endif
  6.  
Since the WCHAR is used you cant typecast it to char*.
So dont define Unicode and after that u can convert to char*

Raghuram
Jun 30 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
You can't use sscanf(). Nor can you use char or char*:
char filename[128]; <--- ERROR
char ext[128]; <--- ERROR
You have to use TCHAR.
Expand|Select|Wrap|Line Numbers
  1. TCHAR filename[128];
  2. TCHAR ext[128];
  3.  

sscanf() only works with char. When Unicode is defined it has to work with wchar_t, which is WCHAR. TCHAR maps between char and wchar_t based on the Unicode settin gin your build.

The TCHAR compatible sscanf() is _stscanf().

Read this: http://msdn.microsoft.com/en-us/library/ms860358.aspx.

Your code should be:
Expand|Select|Wrap|Line Numbers
  1. if (_stscanf_s (ffd.cFileName, TEXT("%16s.%3s", filename, ext) == 2)
  2. {
  3.  
And lastly, the printf() have to be got rid of:
Expand|Select|Wrap|Line Numbers
  1. _tprintf(TEXT("File Name : %s\n"), filename);
  2. _tprintf(TEXT("Extension : %s\n"), ext);
  3. etc...
  4.  
Remember, with TCHAR you cannot ever use the native type char or wchat_t.
Jun 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
11522
by: Abhijit Bhadra | last post by:
Hi , I was trying to build my project in VC with latest Service Packs but got this error . C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE\atlconv.h(125) : error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
7
5327
by: GRoll35 | last post by:
This is 1 source page of a project im working on. i just have this one error. i'll show the error then show the code.. i'll point out the line that it doesn't like. if anyone has any ideas or advise that would be great. thank you very much! (42): error C2664: 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'char ' to 'char *' with
12
10073
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
1
7985
by: fheinstein | last post by:
Hello, I am a newbies in .NET and i am followin a tutorial to create my first MFC program but when a compile a have this problem: See the file attach! Please help Compiling... HelloMFCDlg.cpp
3
13926
by: bg_ie | last post by:
Hi, I am using a API I downloaded from the internet programmed in C. I need the function below which works with this api in my c++ file - void StoreNoteCallback(void *context, int arglen, const void *args, OSCTimeTag when, NetworkReturnAddressPtr returnAddr) { const char *typeArgs = (const char *)args;
1
10319
by: asenthil | last post by:
Hai this is senthil... I had tried to write a string which fetched from a database. into a file... when i tried to compile the solution the following error occurs like this error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char ' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2
9151
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
5
5383
by: slizorn | last post by:
well the error i get is the title above: error C2664: 'searchTree' : cannot convert parameter 2 from 'const char *' to 'char' error is form this line searchTree(treeObj->root ,data1.c_str()); i have also attached searchTree below for yr reference
2
20432
by: slizorn | last post by:
error is as stated in the topic above: error C2440: '=' : cannot convert from 'char *' to 'char' code is below void handleOneLine(string string1) { char * cstr, *p; int counter; string str (string1);
0
8496
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4024
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1594
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1329
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.