Help: Error C2664 during compiling

Hi all, I want to save my algorithm result to a file so that the data can be used in future.Unfortunately i failed to compile my code and hope you guys can help me out and tell me the mistake.Thanks.

The error shown as below:
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

My code as below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
bool CSOINN::SaveNetworkData(const char *fileName)
{
	HANDLE hFile;
	DWORD dwWriteSize;
	int i, nodeNum, edgeNum;

	hFile = CreateFile(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		CloseHandle(hFile);
		return false;
	}

	// SOINN data
	WriteFile(hFile, &(this->m_dimension), sizeof(int), &dwWriteSize, NULL);// m_dimension
	WriteFile(hFile, &(this->m_lambda), sizeof(int), &dwWriteSize, NULL);	// m_lambda
	WriteFile(hFile, &(this->m_ageMax), sizeof(int), &dwWriteSize, NULL);	// m_ageMax
	WriteFile(hFile, &(this->m_classNum), sizeof(int), &dwWriteSize, NULL);	// m_classNum
	WriteFile(hFile, &(this->m_inputNum), sizeof(int), &dwWriteSize, NULL);	// m_inputNum

	// Node data
	nodeNum = (int)m_nodeInfo.size();
	WriteFile(hFile, &nodeNum, sizeof(int), &dwWriteSize, NULL);// nodeNum
	for (i=0; i<nodeNum; i++)
	{
		WriteFile(hFile, this->m_nodeInfo[i].m_signal, sizeof(double)*m_dimension, &dwWriteSize, NULL);	//m_signal
		WriteFile(hFile, this->m_nodeInfo[i].m_neighbor, sizeof(int)*CNode::MAX_NEIGHBOR, &dwWriteSize, NULL);	//m_neighbor
		WriteFile(hFile, &(this->m_nodeInfo[i].m_neighborNum), sizeof(int), &dwWriteSize, NULL);		//m_neighborNum
		WriteFile(hFile, &(this->m_nodeInfo[i].m_learningTime), sizeof(int), &dwWriteSize, NULL);		//m_learningTime
		WriteFile(hFile, &(this->m_nodeInfo[i].m_classID), sizeof(int), &dwWriteSize, NULL);			// m_classID
		//WriteFile(hFile, &(this->m_nodeInfo[i].m_dimension), sizeof(int), &dwWriteSize, NULL);		//m_dimension
	}

	// Edge data
	edgeNum = (int)m_edgeInfo.size();
	WriteFile(hFile, &edgeNum, sizeof(int), &dwWriteSize, NULL);// edgeNum
	for (i=0; i<edgeNum; i++)
	{
		WriteFile(hFile, &(this->m_edgeInfo[i].m_from), sizeof(int), &dwWriteSize, NULL);	// m_from
		WriteFile(hFile, &(this->m_edgeInfo[i].m_to), sizeof(int), &dwWriteSize, NULL);		// m_to
		WriteFile(hFile, &(this->m_edgeInfo[i].m_age), sizeof(int), &dwWriteSize, NULL);	// m_age
	}

	CloseHandle(hFile);
	return true;
}

int _tmain(int argc, _TCHAR* argv[]){
SaveNetworkData(test);
return 0;
}
Last edited on
CreateFile takes TCHARs
CreateFileW takes wchar_ts
CreateFileA takes chars


Since you are giving it a char string (const char*), you want to use CreateFileA, not CreateFile.

Note that this applies to any and all WinAPI functions/structs which use strings. Including but not limited to MessageBox (MessageBoxA, MessageBoxW), DeleteFile, etc.
Last edited on
Hi Disch, Thanks for your reply, I have changed my code to CreateFileA and there is no compilation error. Then, i have added few new codes in main loop to test my SaveNetworkData function as below:

1
2
3
4
5
6
7
8
int _tmain(int argc, _TCHAR* argv[]){

char *testfile;

SaveNetworkData(testfile);

return 0;
}


Another issue is raised up when i testing the code, i noticed that my program will always go to this condition:

if (hFile == INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
return false;
}

and couldn't save my algorithm data. Can you help me to check again and guide me how to solve this problem.

Thanks.
Last edited on
As a suggestion, you could add code to retrieve the last error with GetLastError() which may help you determine why you get an invalid handle.
Add something like the below (I've not tested this though) inside your if statment before you close the file.

1
2
3
4
5
6
char	szBuf[280]; 
LPVOID	lpMsgBuf;
DWORD	dw = GetLastError();
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,dw,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPSTR) &lpMsgBuf,0, NULL );
sprintf(szBuf,"%s failed with error %d: %s",from, dw, lpMsgBuf);
LocalFree(lpMsgBuf);


szBuf should contain the error number and message.
Dear Moooce, thanks for your reply. the code is not working and there is an error as below:

error C2065: 'from' : undeclared identifier



Can you give me some hints how to solve it?

Thanks
sorry, my bad, I took this from some other code. Just remove the referance to from and the first %s.

sprintf(szBuf,"failed with error %d: %s", dw, lpMsgBuf);

Should do it. (although the code is a bit C based and not so C++ it should help you narrow down cause)
Dear mooce, i have modified the code and no error for compiling. but when i run in console there is nothing to come out. Is that normal?

Thanks
szBuf is a C string containing the error text.
You can cout it to the console or printf() it, or look at it in the debug watch variables. Up to you...
Topic archived. No new replies allowed.