Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project that involves parsing a bunch of xml files in a customizable folder.
For sake of saving you several hundred lines of code...
Im using MFC on windows XP to build a dialog app, CString is my primary means of storing string data, all global app settings are in a static class instance AppSettings (appropriately). The problem is, that the user defined folder is stored in a CString, I then need to take that base folder, append a sub folder to it, and create a file in that subfolder, or overwrite it if it exists.
So far I have the writing to file done. As well as creating the subfolder, problem is that when i try to write to a file in the newly created folder, I get crashes. Upon examining, I use CreateDirectory() to create the folder, and it puts the read-only attribute on the folder. The end user is not going to want to have to change all these permissions each time they add an entry (which creates a subfolder). So how do I either A) after creating the folder, change its permissions to writable..or B) set the permisions of the new folder during creation. Right now Im doing this:
C++
folderpath.Format(L"%s\\%s_%s_%i", 
App_Settings.ClientFolder.GetString(), // client folder
                                m_Clients.at(i).getLastName().GetString(), // first name
                                m_Clients.at(i).getFirstName().GetString(), // last name
                                m_Clients.at(i).getID()); // id

// check if directory exists
DWORD res = GetFileAttributes(folderpath.GetString());
if (res == INVALID_FILE_ATTRIBUTES)
{
	// need to create the directory
	if (!CreateDirectory(folderpath.GetString(),NULL))
	{
		// failed to create directory
		return false;
	}
	// make sure folder is writeable
	// ?
}

This is in a loop that counts through each of the clients in a vector list, and creates a folder for each one, then writes client data to a file in each respective directory.
Im thinking that it has something to do with LPSECURITY_ATTRIBUTES being set to NULL. But the documentation for the structure is useless to me. Google turns up minimal relevant information about CreateDirectory as well. Any help at this point would be appreciated. Thanks
Posted

I have tried to re-create your problem on Windows 7, and am able to create a new directory, add a file to that directory, and write data into that file. Windows explorer shows the directory as read only but I suspect that this is not absolutely correct. You state that you get crashes in your program, but are you sure they are the result of this action? Try stepping through the code with the debugger to see if you can identify exactly where the crash occurs.
 
Share this answer
 
A directory is always treated as read-only (internally it's sorta like a read-only file entry if you think of it). As Richard hinted already, your error is somewhere else.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900