C语言 文件读写 ferror 函数 - C语言零基础入门教程
生活随笔
收集整理的這篇文章主要介紹了
C语言 文件读写 ferror 函数 - C语言零基础入门教程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一.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 fileif((f=fopen("includehelp.txt","r"))==NULL){printf("Cannot open the file...");//if not exist program is terminatedexit(1);}// Check if here is some error in the fileif(ferror(f))printf("Error to read the file\n");elseprintf("No error in reading\n");printf("File content is--\n");//print the strings until EOF is encounteredwhile(!feof(f)){fgets(str,100,f);//print the stringprintf("%s",str);}//close the opened filefclose(f);return 0; }應該注意,對同一個文件(文件指針或文件描述符)每一次調用讀 fread /寫 fwrite 等操作函數,均產生一個新的 ferror 函數值,因此,應當在調用讀 fread /寫 fwrite 等函數后立即檢查 ferror 函數的值,否則信息會丟失。
三.猜你喜歡
未經允許不得轉載:猿說編程 ? C 語言 文件讀寫 ferror 函數
總結
以上是生活随笔為你收集整理的C语言 文件读写 ferror 函数 - C语言零基础入门教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python break/continu
- 下一篇: jq之blur