How to delete file of the current user in C/C++? -
i want delete file user logged in using function deletefile()
library , i'm not getting ...
i tried this:
deletefile ("c: \ \ users \ \% username% \ \ file");
also tried capture user name this:
tchar name [unlen + 1]; unlen dword size = + 1; getusername (name, & size);
but did not know put variable name
function deletefile()
.
the clean way user's profile directory use shgetspecialfolderpath api appropriate csidl code (in case csidl_profile). here short (untested) example:
char the_profile_path[max_path]; if (shgetspecialfolderpath(null, the_profile_path, csidl_profile, false) == false) { cerr << "could not find profile path!" << endl; return; } std::ostringstream the_file; buffer << the_profile_path << "\\file"; if (deletefile(buffer.c_str()) == true) { cout << buffer << " deleted" << endl; } else { cout << buffer << " not deleted, lasterror=" << getlasterror() << endl; }
every other way "construct" user's profile path or other special folder of windows lead serious troubles. example reduces portability of application if profile location changes in future version (as happened between windows xp , vista), or if parts of path language dependent (shouldn't issue anymore since vista think) or user relocates profiles (could issue in administrated environments, etc.
please note place supposed create files application not profile's root path appdata or localappdata (both can queried using appropriate csidl) folders.
Comments
Post a Comment