HELP: Failed to create text file

Hi all, I'm new to c++ programming and i'm stuck with a problem of creating new text file. My situation is I can compile my codes without any error but when i run the program it failed to create a text file.

Below is my code:

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
bool CSOINN::SaveNetworkData(void)
{
	HANDLE hFile;
	DWORD dwWriteSize;

	int i, nodeNum, edgeNum;
	
	hFile = CreateFile(TEXT("C:\fileName.TXT"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{	
		cout <<"Error:Could not create hfile!" <<endl;
		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
	WriteFile(hFile, &(this->QuantizeError), sizeof(double), &dwWriteSize, NULL); //Quantization Error
	cout <<"SOINN data saved!" <<endl;

	// 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
		cout <<"Node data saved!" <<endl;
		//WriteFile(hFile, &(this->m_nodeInfo[i].m_dimension), sizeof(int), &dwWriteSize, NULL);					// m_dimension
	}


The program output is "Error:Could not create hfile!" Hope you guys can give me some hint how to solve this problem.

Thanks.
Try this:

hFile = CreateFile(TEXT("C:\\fileName.TXT"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // Note that '\\'
Hi coder777, thanks for your reply. It still cannot create a text file. Previously my code can work with my laptop. And now I compile in different computer and my code couldn't work. Is the operating system problem?because previously my laptop running windows 7 32 bits and now my desktop running windows7 64 bits.

Thanks.
It may be an os problem because you may not have the appropriate right to create a file at that directory
Topic archived. No new replies allowed.