Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3042

Problem with function rename()

$
0
0
Hello to all. I'm new in this forum. I'm writing a simple function (using MFC) that open a folder, write in a file .txt and rename the folder. This is the code snippet:

CString s, s1, s2;

s.Format("C:\\MyFolder\\test.txt");
CStdioFile cf1;
if (!(cf1.Open(s, CFile::modeWrite | CFile::modeCreate | CFile::typeText)))
{
MessageBox("Error ", "Attention"); return;
}

cf1.WriteString("OK\n");
cf1.Close();

s1.Format("C:\\MyFolder");
s2.Format("C:\\MyFolderRen");

result = rename(s1, s2);
if (result == -1)
{
s1.Format("%s", strerror(errno)); //"Permission denied"
}

rename() function doesn't rename my folder if before I wrote in a file conteined in that folder.
The value returned is -1.
Obviously the function works correctly if I only rename that folder and I don't write into the file. What is the reason of this behavior?
Thanks in advance.

Viewing all articles
Browse latest Browse all 3042

Trending Articles