在 C Visual Studio 2008 中重命名文件

发布于 2025-01-08 20:10:53 字数 1299 浏览 3 评论 0原文

我有以下代码,由于某种原因,rename(..) 函数失败。我尝试提供完整路径,以及更改工作目录,然后仅提供文件名。下面是后者失败的结果,errno13。所有文件都位于此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2025-01-15 20:10:53

我上面的最后一条评论回答了这个问题。

My last comment above answers this question/issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文