473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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("FindFirstFile 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("Extension : %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_cast, 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 13114
gpraghuram
1,275 Expert 1GB
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 Expert Mod 8TB
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
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' :...
7
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...
12
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...
1
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......
3
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,...
1
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:...
2
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
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());...
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.