📜  C / C++中文件操作期间的错误处理

📅  最后修改于: 2021-05-28 05:15:41             🧑  作者: Mango

在C++中从文件读取数据或将数据写入文件时,可能会发生错误,这是很常见的。例如,由于以下原因可能会导致错误:

  • 尝试读取超出指示符的文件时。
  • 尝试读取不存在的文件时。
  • 尝试使用尚未打开的文件时。
  • 尝试以适当的模式使用文件时,即,将数据写入已打开以供读取的文件。
  • 当写入受写保护的文件时,即尝试写入只读文件。

如果没有检查错误,则程序可能会异常运行,因此,未经检查的错误可能会导致程序过早终止或输出不正确。

以下是在C / C++中进行文件操作时的一些错误处理功能:

ferror():

在C / C++中,库函数ferror()用于检查流中的错误。其原型写为:

ferror()函数检查流中是否有错误。如果没有发生错误,则返回零值;如果存在错误,则返回非零值。错误指示将一直持续到关闭文件为止,除非使用clearerr()函数将其清除。

以下是实现使用ferror()的程序

C
// C program to illustrate the
// use of ferror()
#include 
#include 
  
// Driver Code
int main()
{
    FILE* fp;
  
    // If a file is opened which does
    // not exist, then it will be an
    // error and corresponding errno
    // value will be set
    char feedback[100];
  
    int i;
    fp = fopen("GeeksForGeeks.TXT", "w");
  
    if (fp == NULL) {
        printf("\n The file could "
               "not be opened");
        exit(1);
    }
  
    printf("\n Provide feedback on "
           "this article: ");
    fgets(feedback, 100, stdin);
  
    for (i = 0; i < feedback[i]; i++)
        fputc(feedback[i], fp);
  
    // Error writing file
    if (ferror(fp)) {
        printf("\n Error writing in file");
        exit(1);
    }
  
    // Close the file pointer
    fclose(fp);
}


C++
// C++ program to illustrate the
// use of ferror()
#include 
  
// Driver Code
int main()
{
    FILE* fp;
  
    // If a file is opened which does
    // not exist, then it will be an
    // error and corresponding errno
    // value will be set
    char feedback[100];
  
    int i;
    fp = fopen("GeeksForGeeks.TXT", "w");
  
    if (fp == NULL) {
        printf("\n The file could "
               "not be opened");
        exit(1);
    }
  
    printf("\n Provide feedback on "
           "this article: ");
    fgets(feedback, 100, stdin);
  
    for (i = 0; i < feedback[i]; i++)
        fputc(feedback[i], fp);
  
    // Error writing file
    if (ferror(fp)) {
        printf("\n Error writing in file");
        exit(1);
    }
  
    // Close the file pointer
    fclose(fp);
}


C
// C program to illustrate the
// use of clearerr()
#include 
#include 
#include 
  
// Driver Code
int main()
{
    FILE* fp;
    char feedback[100];
  
    char c;
  
    fp = fopen("file.txt", "w");
  
    c = fgetc(fp);
    if (ferror(fp)) {
        printf("Error in reading from"
               " file : file.txt\n");
    }
    clearerr(fp);
  
    if (ferror(fp)) {
        printf("Error in reading from "
               "file : file.txt\n");
    }
  
    // close the file
    fclose(fp);
}


C++
// C++ program to illustrate the
// use of clearerr()
#include 
  
// Driver Code
int main()
{
    FILE* fp;
    char feedback[100];
  
    char c;
  
    fp = fopen("file.txt", "w");
  
    c = fgetc(fp);
    if (ferror(fp)) {
        printf("Error in reading from"
               " file : file.txt\n");
    }
    clearerr(fp);
  
    if (ferror(fp)) {
        printf("Error in reading from "
               "file : file.txt\n");
    }
  
    // close the file
    fclose(fp);
}


C
// C program to illustrate the
// use of perror()
#include 
#include 
#include 
  
// Driver Code
int main()
{
    FILE* fp;
  
    // First rename if there is any file
    rename("file.txt", "newfile.txt");
  
    // Now try to open same file
    fp = fopen("file.txt", "r");
  
    if (fp == NULL) {
  
        perror("Error: ");
        return (-1);
    }
  
    // Close the file pointer
    fclose(fp);
  
    return (0);
}


C++
// C++ program to illustrate the
// use of perror()
#include 
#include 
  
// Driver Code
int main()
{
    FILE* fp;
  
    // First rename if there is any file
    rename("file.txt", "newfile.txt");
  
    // Now try to open same file
    fp = fopen("file.txt", "r");
  
    if (fp == NULL) {
  
        perror("Error: ");
        return (-1);
    }
  
    // Close the file pointer
    fclose(fp);
  
    return (0);
}


输出:

说明:执行完此代码后,屏幕上将显示“提供有关本文的反馈:”,并给出一些反馈后,屏幕上将显示“过程在几秒钟后退出,返回值为0”。

clearerr():

函数clearerr()用于清除流的文件结尾和错误指示符。其原型可以给出为:

clearerr()清除流指向的流的错误。使用该函数是因为错误指示不会自动清除。设置了特定流的错误指示符后,对该流的操作将继续返回错误值,直到调用clearerr()fseek()fsetpos()rewind()为止。

以下是实现使用clearerr()的程序

C

// C program to illustrate the
// use of clearerr()
#include 
#include 
#include 
  
// Driver Code
int main()
{
    FILE* fp;
    char feedback[100];
  
    char c;
  
    fp = fopen("file.txt", "w");
  
    c = fgetc(fp);
    if (ferror(fp)) {
        printf("Error in reading from"
               " file : file.txt\n");
    }
    clearerr(fp);
  
    if (ferror(fp)) {
        printf("Error in reading from "
               "file : file.txt\n");
    }
  
    // close the file
    fclose(fp);
}

C++

// C++ program to illustrate the
// use of clearerr()
#include 
  
// Driver Code
int main()
{
    FILE* fp;
    char feedback[100];
  
    char c;
  
    fp = fopen("file.txt", "w");
  
    c = fgetc(fp);
    if (ferror(fp)) {
        printf("Error in reading from"
               " file : file.txt\n");
    }
    clearerr(fp);
  
    if (ferror(fp)) {
        printf("Error in reading from "
               "file : file.txt\n");
    }
  
    // close the file
    fclose(fp);
}

输出:

函数perror()代表打印错误。如果发生错误,程序员可以使用perror()函数确定发生的错误的类型。调用perror()时,它将显示一条消息,描述在库函数调用或系统调用期间发生的最新错误。其原型可以给出为:

  • perror()使用一个参数,该参数指向可选的用户定义消息,该消息首先打印,后跟冒号和描述最近错误的实现定义消息。
  • 如果在实际未发生任何错误的情况下调用perror(),则将显示“无错误”。
  • 要记住的最重要的事情是对perror()的调用并没有采取任何措施来处理错误情况,这完全取决于程序采取的措施。例如,程序可能会提示用户执行某些操作,例如终止程序。
  • 通常,程序的活动将通过检查errno的值和错误的性质来确定。
  • 为了使用外部常量errno ,必须包含头文件ERRNO.H

下面是下面给出的程序,说明了perror()的用法。在此,假设文件“ file.txt”不存在。

C

// C program to illustrate the
// use of perror()
#include 
#include 
#include 
  
// Driver Code
int main()
{
    FILE* fp;
  
    // First rename if there is any file
    rename("file.txt", "newfile.txt");
  
    // Now try to open same file
    fp = fopen("file.txt", "r");
  
    if (fp == NULL) {
  
        perror("Error: ");
        return (-1);
    }
  
    // Close the file pointer
    fclose(fp);
  
    return (0);
}

C++

// C++ program to illustrate the
// use of perror()
#include 
#include 
  
// Driver Code
int main()
{
    FILE* fp;
  
    // First rename if there is any file
    rename("file.txt", "newfile.txt");
  
    // Now try to open same file
    fp = fopen("file.txt", "r");
  
    if (fp == NULL) {
  
        perror("Error: ");
        return (-1);
    }
  
    // Close the file pointer
    fclose(fp);
  
    return (0);
}

输出:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。