目錄
- 一.ferror 函式簡介
- 二.ferror 函式實戰
- 三.猜你喜歡
零基礎 C/C++ 學習路線推薦 : C/C++ 學習目錄 >> C 語言基礎入門
一.ferror 函式簡介
C 語言 ferror 函式用于檢測檔案讀寫程序中是否有產生錯誤,宣告如下:
#include <stdio.h>
/*
*描述:寫入資料到緩沖區檔案
*
*引數:
* [in] stream:檔案指標句柄
*
*回傳值:對檔案讀寫時出錯時,檔案就會產生錯誤標志!如果出現讀寫錯誤,回傳非 0 值,如果沒有讀寫錯誤,回傳 0
*/
int ferror(FILE *stream);
應該注意,對同一個檔案(檔案指標或檔案描述符)每一次呼叫讀 fread /寫 fwrite 等操作函式,均產生一個新的 ferror 函式值,因此,應當在呼叫讀 fread /寫 fwrite 等函式后立即檢查 ferror 函式的值,否則資訊會丟失,
二.ferror 函式實戰
/******************************************************************************************/
//@Author:猿說編程
//@Blog(個人博客地址): www.codersrc.com
//@File:C語言教程 - C語言 檔案讀寫 ferror 函式
//@Time:2021/07/22 07:30
//@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
/******************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
char str[100];
//Check the existence of that file
if((f=fopen("includehelp.txt","r"))==NULL){
printf("Cannot open the file...");
//if not exist program is terminated
exit(1);
}
// Check if here is some error in the file
if(ferror(f))
printf("Error to read the file\n");
else
printf("No error in reading\n");
printf("File content is--\n");
//print the strings until EOF is encountered
while(!feof(f)){
fgets(str,100,f);
//print the string
printf("%s",str);
}
//close the opened file
fclose(f);
return 0;
}
應該注意,對同一個檔案(檔案指標或檔案描述符)每一次呼叫讀 fread /寫 fwrite 等操作函式,均產生一個新的 ferror 函式值,因此,應當在呼叫讀 fread /寫 fwrite 等函式后立即檢查 ferror 函式的值,否則資訊會丟失,
三.猜你喜歡
- C 語言 陣列下標越界和記憶體溢位區別
- C 語言 使用指標遍歷陣列
- C 語言 指標和陣列區別
- C 語言 指標陣列和陣列指標區別
- C 語言 野指標
- C 語言 函式值傳遞和址傳遞
- C 語言 函式不定長引數
- C 語言 函式指標
- C 語言 指標函式
- C 語言 回呼函式 callback
- C 語言 #pragma once
- C 語言 #include <> 與 #include “” 區別
- C 語言 const 修飾函式引數
- C 語言 const 和 define 區別
- C 語言 #運算子
- C 語言 ##運算子
- C 語言 __VA_ARGS__
- C 語言 ##__VA_ARGS__
- C 語言 函式不定長引數 ##__VA_ARGS__經典案例
- C 語言 va_start / va_end / va_arg 自定義 printf 函式
- C 語言 main 函式
- C 語言 main 函式引數 main(int argc, char *argv[])
- C 語言 區域變數
- C 語言 全域變數
- C 語言 全域變數和區域變數區別
- C 語言 static
未經允許不得轉載:猿說編程 ? C 語言 檔案讀寫 ferror 函式
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/308235.html
標籤:其他
