rename
來自 cppreference.com
定義於標頭檔案 <stdio.h> |
||
int rename( const char* old_filename, const char* new_filename ); |
||
更改檔案的檔名。檔案由 old_filename 指向的字串標識。新檔名由 new_filename 指向的字串標識。
如果 new_filename 存在,則行為是實現定義的。
目錄 |
[編輯] 引數
old_filename | - | 指向包含要重新命名檔案的路徑的空終止字串的指標 |
new_filename | - | 指向包含檔案新路徑的空終止字串的指標 |
[編輯] 返回值
成功時返回 0,錯誤時返回非零值。
[編輯] 注意
POSIX 指定了此函式語義的許多額外細節。
[編輯] 示例
執行此程式碼
#include <stdio.h> #include <stdlib.h> int main(void) { FILE* fp = fopen("from.txt", "w"); // create file "from.txt" if (!fp) { perror("from.txt"); return EXIT_FAILURE; } fputc('a', fp); // write to "from.txt" fclose(fp); int rc = rename("from.txt", "to.txt"); if (rc) { perror("rename"); return EXIT_FAILURE; } fp = fopen("to.txt", "r"); if(!fp) { perror("to.txt"); return EXIT_FAILURE; } printf("%c\n", fgetc(fp)); // read from "to.txt" fclose(fp); return EXIT_SUCCESS; }
可能的輸出
a
[編輯] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.21.4.2 rename 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.21.4.2 rename 函式 (p: TBD)
- C11 標準 (ISO/IEC 9899:2011)
- 7.21.4.2 rename 函式 (p: 302-303)
- C99 標準 (ISO/IEC 9899:1999)
- 7.19.4.2 rename 函式 (p: 268-269)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.9.4.2 rename 函式
[編輯] 參見
擦除檔案 (function) | |
C++ 文件 關於 rename
|