C語言文件操作之fgets()

http://blog.csdn.net/daiyutage/article/details/8540932

來說一說fgets(..)函數。

? ??原型 ?char * ?fgets(char * s, int n,FILE *stream);

? ? 參數:

? ? ? ? ?s: 字符型指針,指向存儲讀入數據的緩沖區的地址。

? ? ? ? ?n: 從流中讀入n-1個字符

? ? ? ? ?stream : 指向讀取的流。

? ?返回值:

? ? ? ? ? 1. 當n<=0 時返回NULL,即空指針。

? ? ? ? ? 2. 當n=1 時,返回空串"".

? ? ? ? ? 3. 如果讀入成功,則返回緩沖區的地址。

? ? ? ? ? 4. 如果讀入錯誤或遇到文件結尾(EOF),則返回NULL.

? ? ? ? ? 看看這個函數的官方說明:

? ? ? ? ? ? ? ? ? ? ???/***?
? ? ? ? ? ? ? ? ? ? *char *fgets(string, count, stream) - input string from a stream?
? ? ? ? ? ? ? ? ? ? *?
? ? ? ? ? ? ? ? ? ? *Purpose: ?
? ? ? ? ? ? ? ? ? ? * get a string, up to count-1 chars or '\n', whichever comes first,?
? ? ? ? ? ? ? ? ? ? * append '\0' and put the whole thing into string. the '\n' IS included?
? ? ? ? ? ? ? ? ? ? * in the string. if count<=1 no input is requested. if EOF is found?
? ? ? ? ? ? ? ? ? ? * immediately, return NULL. if EOF found after chars read, let EOF?
? ? ? ? ? ? ? ? ? ? * finish the string as '\n' would.?
? ? ? ? ? ? ? ? ? ? *?
? ? ? ? ? ? ? ? ? ? *Entry:?
? ? ? ? ? ? ? ? ? ? * char *string - pointer to place to store string?
? ? ? ? ? ? ? ? ? ? * int count - max characters to place at string (include \0)?
? ? ? ? ? ? ? ? ? ? * FILE *stream - stream to read from?
? ? ? ? ? ? ? ? ? ? *?
? ? ? ? ? ? ? ? ? ? *Exit:?
? ? ? ? ? ? ? ? ? ? * returns string with text read from file in it.?
? ? ? ? ? ? ? ? ? ? * if count <= 0 return NULL?
? ? ? ? ? ? ? ? ? ? * if count == 1 put null string in string?
? ? ? ? ? ? ? ? ? ? * returns NULL if error or end-of-file found immediately?
? ? ? ? ? ? ? ? ? ? *?
? ? ? ? ? ? ? ? ? ? *Exceptions:?
? ? ? ? ? ? ? ? ? ? *?
? ? ? ? ? ? ? ? ? ? *******************************************************************************/?

????????????標準庫中fgets(...)的實現:

? ? ? ? ? ??

[cpp]?view plain?copy
  1. /****************************************************??
  2. ?char?*fgets(char?*s,?int?n,??FILE?*stream)??
  3. ???{??
  4. ?????register?int?c;??
  5. ?????register?char?*cs;??
  6. ?????cs=s;??
  7. ?????while(--n>0?&&(c?=?getc(stream))!=EOF)??
  8. ?????if?((*cs++=??c)?=='\n')??
  9. ???????????break;??
  10. ?????*cs?='\0';??
  11. ?????return?(c?==?EOF?&&?cs?==?s)??NULL?:s?;??
  12. ????}??
  13. ??
  14. /********************************************************??



? ? ? ?在用fgets(..)讀入數據時,先定義一個字符數組或字符指針,如果定義了字符指針 ,那么一定要初始化。

? ? ? ? example:

? ? ? ? ? ? ? char s[100]; //可以。

? ? ? ? ? ? ? char *s; ?//不可以,因為只是聲明了一個指針。但并沒有為它分配內存緩沖區。

? ? ? ? 所以,如果要用指針,則 ?char *s=(char *)malloc(100*sizeof(char)); 為其分配內存空間,c++中用char *s=new char [100]; ? ? ?如果為分配內存空間,編譯時不會檢查出問題,但運行時會出現未知錯誤。。

? ? ? ??fgets(...)讀入文本行時的兩種情況。

? ? ? ? ? 1.如果n大于一行的字符串長度,那么當讀到字符串末尾的換行符時,fgets(..)會返回。并且在s的最后插入字符串結束標志'\0'。 而s緩沖區剩余的位置不會再填充。

? ? ? ? ? ?example:

? ? ? ? ? ? ? 123abc

? ? ? ? ? ? ? fgets(s,10,fp);

? ? ? ? ? ? ? 此時,讀入七個字符,123abc\n,實際上還有最后的'\0',所以,strlen(s)=7; 如果要去除末尾的\n,s[strlen(s)-1]='\0';便可。

? ? ? ? ? 2.如果n小于等于一行的字符串的長度,那么讀入n-1個字符,此時并沒有讀入\n因為并沒有到行尾 ,同樣在最后會插入'\0'.

? ? ? ? ? example:

? ? ? ? ? ? 123abc

? ? ? ? ? ? char ?s[5];

? ? ? ? ? ? fgets(s,5,fp);

? ? ? ? ? ? 這時讀入4個字符,123a,并沒有換行符,所以strlen(s)=4.

? ? ? ?fgets(...)讀入整個文件內容

? ? ? ? ? 通常用while()循環來使fges()讀入文本全部內容,并按行讀入。

? ? ? ? ??

[cpp]?view plain?copy
  1. char?s[1024];??
  2. ???????????while((fgets(s,1024,fp))!=NULL)??
  3. ????????????{??
  4. ??????????????????printf(s);??
  5. ????????????}??

? ? ? ? ?當然如果n小于每行的字符個數,也可以讀,只不過讀的次數要多。

? ? ? ? ? 假設一行為 : 123456789

? ? ? ? ??

[cpp]?view plain?copy
  1. char?s[2];??
  2. ??????????int??num=0;??
  3. ??????????while((fgets(s,2,fp))!=NULL)??
  4. ????????????{??
  5. ?????????????????printf(s);??
  6. ?????????????????n++;??
  7. ????????????}??

? ? ? ? ? 每次讀入一個字符, 最后也會讀完一行,num=10,讀了十次,所以,fgets若沒遇到換行符,會接著從前一次的位置繼續讀入n-1個字符,只要是文本流沒關閉。

? ? ? ? ?讀入空行的情況:

? ? ? ? ? ? 第一行 ? abcdef123

? ? ? ? ? ? 第二行 ? ? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? 第三行 ?helloworld

? ? ? ? ? ? 其中第二行為空,fget(..)會把第二行也讀入,因為并未到文件結尾。

? ? ? ? ? ? 有時我們并不需要空行,可以這樣做。

? ? ? ? ??

[cpp]?view plain?copy
  1. while((fgets(s,n,fp))!=NULL)??
  2. ?????????????{??
  3. ??????????????????if(strlen(s)!=1)????//注意這兒是1不是0,因為盡管是空行,它也會讀入換行符,strlen(s)=1;??
  4. ??????????????????????printf(s);??
  5. ?????????????}??

? ? ? ???fgets(...)從標準設備讀數據。

? ? ? ? ? ? 用fgets(...)還也讀入標準輸入設備(一般為鍵盤)的信息

? ? ? ? ? ? 原型 ?: ?fgets(s,n,stdin);

? ? ? ? ? ? 假設在控制臺下,我們可以用fgets(...)替代gets(),讀入鍵盤輸入的信息,fgets()是安全的,因為不會像gets()有溢出的可能。。

? ? ? ? ? ? 比如 :輸入 abc

? ? ? ? ? ? fgets(s,n,stdin)也會讀入n-1個字符。但是只是從stdin流讀入。。。

? ? ? ? ? ??



本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384177.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384177.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384177.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

指針與零的比較以及浮點型與零的比較

指針和零的比較 int *p null;if(p ! null){p 20; } 整形和零的比較 int i 0; if(0i) {... } 浮點型和零的比較 判斷一個浮點數是不是零 #define EXP 0.0000000000001 float f 0.00001; if((f > -EXP)&&(f < EXP)) {... } 擴展后 判斷一個浮點數是不…

CodeForces 1138B暴力+剪枝

【題目鏈接】Circus 【題目分析】理解題意以后發現并沒有什么思路&#xff0c;沒有什么算法能用&#xff0c;這個時候就應該想到計算機解題的本質——暴力求解。相應的就要想到剪枝的條件&#xff0c;肯定不能盲目的暴力求解。 總共有四種人&#xff1a;00,01,10,11&#xff0c…

MYSQL錯誤代碼#1045 Access denied for user 'root'@'localhost'

http://blog.csdn.net/lykezhan/article/details/70880845 遇到MYSQL“錯誤代碼#1045 Access denied for user rootlocalhost (using password:YES)” 需要重置root賬號權限密碼&#xff0c;這個一般還真不好解決。 不過&#xff0c;這幾天調試的時候真的遇到了這種問題&#x…

C語言操作符

移位表達式 左移操作符<< 左邊拋棄,右邊補零 右移操作符>> 1.邏輯右移 左邊補零,右邊丟棄 2.算數右移 左邊補符號位,右邊丟棄 注意: 1.左移一位相當于乘2,右移一位相當于除2,并且在內存中存放的是二進制的補碼,且移位操作符只對int型數操作 2.移位操作符不要移動…

棋盤問題——DFS

【題目描述】 在一個給定形狀的棋盤&#xff08;形狀可能是不規則的&#xff09;上面擺放棋子&#xff0c;棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列&#xff0c;請編程求解對于給定形狀和大小的棋盤&#xff0c;擺放k個棋子的所有可行的擺放方…

linux安裝mysql和使用c語言操作數據庫的方法 c語言連接mysql

http://www.jb51.net/article/46139.htm 1. MySQL的安裝與配置&#xff1a; 在Ubuntu下安裝MySQL方法很簡單&#xff0c;使用如下命令&#xff1a; 復制代碼 代碼如下:sudo apt-get install mysql-server安裝的過程中系統會提示設置root密碼&#xff0c;此過程可以跳過&#…

常量變量以及循環

常量 1.三目運算詞 三字母詞表達字符???([??)]??<{??>} 2.循環 1).數組元素以及變量在內存中的分配順序 2)goto語句應用 //電腦關機程序 #include<stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> int ma…

Dungeon Master——BFS

【題目描述】 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move …

Linux 環境 C語言 操作MySql 的接口范例

http://www.cnblogs.com/wunaozai/p/3876134.html 接上一小節&#xff0c;本來是計劃這一節用來講數據庫的增刪改查&#xff0c;但是在實現的過程中&#xff0c;出現了一點小問題&#xff0c;也不是技術的問題&#xff0c;就是在字符界面上比較不好操作。比如要注冊一個帳號&a…

二進制邏輯運算符有關練習題

//1.寫一個函數返回參數二進制中 1 的個數 #include<stdio.h> int div 0; //除數 int rem 0; //余數 int count 0; //計1 int count_one_bits(unsigned int div) {int con 0; //商while (div > 1){con div / 2;rem div % 2;div con;if (1 rem){count;}}…

Catch That Cow——BFS

【題目描述】 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo…

利用mysql提供的c語言接口操作數據庫

http://blog.csdn.net/bladeandmaster88/article/details/52980872 //1.工程要在c/c->常規->附加包含目錄添加mysql.h的路徑D:\mysql5.5\include //2.工程要在鏈接器->常規->附加庫目錄添加libmysql.lib的路徑D:\mysql5.5\lib #include <WinSock2.h>/…

數組相關運算

數組的初始化 數組及指針在內存中的存儲 一維數組在內存中的存儲 有關數組的運算 //一維數組 int a[] {1,2,3,4}; printf("%d\n",sizeof(a));//16這里的a表示的是整個數組,計算出的是整個數組的大小,單位為byte printf("%d\n",sizeof(a 0));/*a沒有單獨…

Find The Multiple——簡單搜索+大膽嘗試

【題目描述】 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more t…

C語言操作MYSQL小例子

http://blog.csdn.net/small_qch/article/details/8180678 初學使用用C語言操作MYSQL&#xff0c;寫了個小例子&#xff0c;帖上來獻丟人一下&#xff0c;呵呵。 程序很簡單&#xff0c;先連接數據庫&#xff0c;然后向class1表中插入一條數據&#xff0c;最后獲取并輸出整個cl…

Find a way——BFS

【題目描述】 Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in the ce…

用C語言操作MySQL數據庫

http://blog.chinaunix.net/uid-26743670-id-3479887.html 參考MYSQL的幫助文檔整理 這里歸納了C API可使用的函數&#xff0c;并在下一節詳細介紹了它們。請參見25.2.3節&#xff0c;“C API函數描述”。 函數 描述 mysql_affected_rows() 返回上次UPDATE、DELETE或INSERT查…

三字棋

整個游戲可以分為以下幾個環節 1.打印一個玩游戲的菜單 2.玩游戲 (1)玩家走一步 (2)電腦走一步 每走一步對結果進行顯示,其中游戲的結果可分為玩家贏,電腦贏,以及平局 代碼顯示如下 game.c #define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h> #include<stdio.h&…

gets fgets 區別

http://www.cnblogs.com/aexin/p/3908003.html 1. gets與fgets gets函數原型&#xff1a;char*gets(char*buffer);//讀取字符到數組&#xff1a;gets(str);str為數組名。 gets函數功能&#xff1a;從鍵盤上輸入字符&#xff0c;直至接受到換行符或EOF時停止&#xff0c;并將讀取…

Fliptile——搜索+二進制優化

【題目描述】 Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is col…