當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 remove()用法及代碼示例

C 中的 remove() 函數

remove() 函數定義在<stdio.h>頭文件。

原型:

    int remove(const char* filename);

參數: const char *filename

返回類型: int

函數的使用:

當我們處理文件時,有時我們需要擦除或刪除一些文件。在文件處理中,我們使用 remove() 函數來擦除文件。函數 remove() 的原型是int remove(const char* filename);

這裏,filename是必須刪除的文件的名稱。如果文件被成功刪除,則返回零,如果發生錯誤則返回非零。

C 中的 remove() 示例

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* f;
    //Check the existence of that file
    if ((f = fopen("includehelp.txt", "r")) == NULL) {
        printf("File does not exist...\n");
    }
    else {
        printf("File is exist.\n");
    }
    fclose(f);

    //remove file
    if (remove("includehelp.txt"))
        printf("Remove error.....\n");
    else
        printf("File is removed\n");

    //Check the existence of that file
    if ((f = fopen("includehelp.txt", "r")) == NULL) {
        printf("File does not exist...\n");
    }
    else {
        printf("File is exist.\n");
    }

    fclose(f);

    return 0;
}

輸出

remove() example in C language




相關用法


注:本文由純淨天空篩選整理自Souvik Saha大神的英文原創作品 remove() function in C language with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。