I have the below code and for some reason, rename(..)
function is failing. I have tried supplying full paths, as well as changing working directory and then supplying just filenames. Below is the latter which fails and errno
is 13
. All files are in my Projects folder of this win32 console project. I don't understand why it is so difficult to simply change a filename, regardless of what the current working dir is.
//fname_string* points to beginning of actual filename
//filename contains entire output file path
//file_ext* points to .csv portion
//fname will get old filename
fname_string = strrchr( filename, '\\' );
if( fname_string == NULL )
{
fname_string = filename; //no dir supplied, so set it to filename
}
else
{
strncpy( fname, filename, fname_string - filename );
fname[fname_string-filename] = '\0';
chdir(fname);
fname_string++; //now points to filename
read_success = errno; //this succeeds supposedly
}
strcpy( fname, fname_string ); //save old file path
sprintf( file_ext, "_%d.csv", append_esn ); //append_esn = 1234
read_success = rename( fname, fname_string );
read_success = errno; //giving me 13
EDIT: I'm dumb and I was closing the 'input' file, and not the 'output' file. So FYI, file better be closed! The reason why I thought it was closed is because I flush the output file and sometimes it didn't have a zero size file.
So rename can take a full path or just the filenames if its in the working directory...There goes two hours of nonsense.... So the above would work, or just doing the below assuming same pointers mentioned above:
strcpy( fname, filename ); //save old file path
sprintf( file_ext, "_%d.csv", append_esn );
read_success = rename( fname, filename );