C語言的getc()函數(shù)和gets()函數(shù)的使用對比
C語言getc()函數(shù):從流中讀取字符
頭文件:
#include <stdio.h>
函數(shù)getc()用于從流中取字符,其原型如下:
int getc(FILE *stream);
【參數(shù)】參數(shù)*steam為要從中讀取字符的文件流。
【返回值】該函數(shù)執(zhí)行成功后,將返回所讀取的字符。
【說明】若從一個文件中讀取一個字符,讀到文件尾而無數(shù)據(jù)時便返回EOF。getc()與fgetc()作用相同,但在某些庫中getc()為宏定義,而非真正的函數(shù)。
【實例】下面的示例演示了getc()函數(shù)的使用,在程序中采用該函數(shù)從標準輸入控制臺中讀取字符,代碼如下。
#include <stdio.h> //引入標準輸入輸出庫
void main( ) {
char ch;
printf ("Input a character: "); //輸入提示信息
ch = getc(stdin); // 從標準輸入控制臺中讀取字符
printf ("The character input was: '%c'\n", ch); // 輸出字符
}
運行上述程序,首先聲明一個用于保存所取字符的變量;然后輸 出提示信息,接收從標準輸入控制臺按下的任意鍵,并將該字符輸出到控制臺。
利用getc()從文件中讀取字符串,代碼如下。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
int ch;
int len;
int i=0;
FILE* fstream;
char msg[100] = "Hello!I have read this file.";
fstream=fopen("test.txt","at+");
if(fstream==NULL)
{
printf("read file test.txt failed!\n");
exit(1);
}
/*getc從文件流中讀取字符*/
while( (ch = getc(fstream))!=EOF)
{
putchar(ch);
}
putchar('\n');
len = strlen(msg);
while(len>0)/*循環(huán)寫入*/
{
putc(msg[i],fstream);
putchar(msg[i]);
len--;
i++;
}
fclose(fstream);
return 0;
}
函數(shù)fopen利用模式“at+”打開文本文件,使用getc從文件流中逐個讀取字符,直到讀完。
C語言gets()函數(shù):從流中讀取字符串
頭文件:
#include <stdio.h>
gets()函數(shù)用于從緩沖區(qū)中讀取字符串,其原型如下:
char *gets(char *string);
gets()函數(shù)從流中讀取字符串,直到出現(xiàn)換行符或讀到文件尾為止,最后加上NULL作為字符串結束。所讀取的字符串暫存在給定的參數(shù)string中。
【返回值】若成功則返回string的指針,否則返回NULL。
注意:由于gets()不檢查字符串string的大小,必須遇到換行符或文件結尾才會結束輸入,因此容易造成緩存溢出的安全性問題,導致程序崩潰,可以使用fgets()代替。
【實例】請看下面一個簡單的例子。
#include <stdio.h>
int main(void)
{
char str[10];
printf("Input a string.\n");
gets(str);
printf("The string you input is: %s",str); //輸出所有的值,注意a
}
如果輸入123456(長度小于10),則輸出結果為:
Input a string. 123456↙ The string you input is:123456
如果輸入12345678901234567890(長度大于10),則輸出結果為:
Input a string. 12345678901234567890↙ The string you input is:12345678901234567890
同時看到系統(tǒng)提示程序已經崩潰。
如果不能正確使用gets()函數(shù),帶來的危害是很大的,就如上面我們看到的,輸入字符串的長度大于緩沖區(qū)長度時,并沒有截斷,原樣輸出了讀入的字符串,造成程序崩潰。
考慮到程序安全性和健壯性,建議用fgets()來代替gets()。
如果你在GCC中使用gets(),編譯無法通過,會提示:
the 'gets' function is dangerous and shout not be used.
C語言gets()函數(shù):從流中讀取字符串
頭文件:
#include <stdio.h>
gets()函數(shù)用于從緩沖區(qū)中讀取字符串,其原型如下:
char *gets(char *string);
gets()函數(shù)從流中讀取字符串,直到出現(xiàn)換行符或讀到文件尾為止,最后加上NULL作為字符串結束。所讀取的字符串暫存在給定的參數(shù)string中。
【返回值】若成功則返回string的指針,否則返回NULL。
注意:由于gets()不檢查字符串string的大小,必須遇到換行符或文件結尾才會結束輸入,因此容易造成緩存溢出的安全性問題,導致程序崩潰,可以使用fgets()代替。
【實例】請看下面一個簡單的例子。
#include <stdio.h>
int main(void)
{
char str[10];
printf("Input a string.\n");
gets(str);
printf("The string you input is: %s",str); //輸出所有的值,注意a
}
如果輸入123456(長度小于10),則輸出結果為:
Input a string. 123456↙ The string you input is:123456
Input a string. 12345678901234567890↙ The string you input is:12345678901234567890
如果不能正確使用gets()函數(shù),帶來的危害是很大的,就如上面我們看到的,輸入字符串的長度大于緩沖區(qū)長度時,并沒有截斷,原樣輸出了讀入的字符串,造成程序崩潰。
考慮到程序安全性和健壯性,建議用fgets()來代替gets()。
如果你在GCC中使用gets(),編譯無法通過,會提示:
the 'gets' function is dangerous and shout not be used.
相關文章
c++中vector<int>和vector<int*>的用法及區(qū)別
這篇文章主要介紹了c++中vector<int>和vector<int*>的用法及區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2013-10-10

