在 C Visual Studio 2008 中重命名文件
我有以下代码,由于某种原因,rename(..)
函数失败。我尝试提供完整路径,以及更改工作目录,然后仅提供文件名。下面是后者失败的结果,errno
为 13
。所有文件都位于此 win32 控制台项目的“Projects”文件夹中。我不明白为什么无论当前工作目录是什么,简单地更改文件名都如此困难。
//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
编辑: 我很愚蠢,我正在关闭“输入”文件,而不是“输出”文件。所以仅供参考,文件最好关闭!我认为它已关闭的原因是因为我刷新了输出文件,有时它没有零大小的文件。
因此,重命名可以采用完整路径,也可以仅采用文件名(如果它位于工作目录中)...有两个小时的废话... 所以上面的方法是可行的,或者假设上面提到的相同的指针执行下面的操作:
strcpy( fname, filename ); //save old file path
sprintf( file_ext, "_%d.csv", append_esn );
read_success = rename( fname, filename );
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 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我上面的最后一条评论回答了这个问题。
My last comment above answers this question/issue.