英文:
How to use filepaths with rename()?
问题
I use the rename() function, but it renames files only in the program directory. How can I point her to a file outside the program folder?
我使用rename()函数,但它只能重命名程序目录中的文件。如何将其指向程序文件夹之外的文件?
I tried to give an input string like
"/home/user/folder/file.txt"
"~/folder/file.txt"
But it still gave an error that the file was not found.
我尝试提供类似的输入字符串
"/home/user/folder/file.txt"
"~/folder/file.txt"
但它仍然报错文件未找到。
#include<stdio.h>
#include<errno.h>
int main(){
char oldn[256]="~/testfolder/oldname.txt";
char newn[256]="~/testfolder/newname.txt";
int ret = rename(oldn, newn);
if(ret!=0){
fprintf(stderr, "Cant rename! Error: %d\n", errno);
perror("error msg");
}
}
英文:
I use the rename() function, but it renames files only in the program directory. How can I point her to a file outside the program folder?
I tried to give an input string like
"/home/user/folder/file.txt"
"~/folder/file.txt"
But it still gave an error that the file was not found.
#include<stdio.h>
#include<errno.h>
int main(){
char oldn[256]="~/testfolder/oldname.txt";
char newn[256]="~/testfolder/newname.txt";
int ret = rename(oldn, newn);
if(ret!=0){
fprintf(stderr, "Cant rename! Error: %d\n", errno);
perror("error msg");
}
}
答案1
得分: 1
"getenv("HOME")" 正在为我工作,以及其他路径如 "home/user/folder/file.txt"。
英文:
Well getenv("HOME") Is working for me, and other paths like "home/user/folder/file.txt".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论