C语言中gets、fgets以及fputs函数

1. gets函数

gets函数是不安全的,不推荐使用。如下代码:

#include 

int  main(void)
{   
    char name[5];                                                                                   
    gets(name);
    puts(name);

    return 0;
}

编译运行:


由此看到编译器给出的警告信息:使用gets函数是不安全的,不推荐使用。


再运行一次,输入较多字符时:


gets不检查预留存储区是否能够容纳实际输入的数据。多出来的字符简单的溢出到相邻的存储区,可能会导致错误。


2. fgets函数

(1)函数原型:

char * fgets ( char * str, int num, FILE * stream );


(2)函数说明:

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the End-of-File is reached, whichever comes first.
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
A null character is automatically appended in str after the characters read to signal the end of the C string.


(3)参数

str Pointer to an array of chars where the string read is stored. num Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used. stream Pointer to a FILE object that identifies the stream where characters are read from.
To read from the standard input, stdin can be used for this parameter.


(4) 返回值

On success, the function returns the same str parameter.
If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned.
If an error occurs, a null pointer is returned.


如下代码:

#include 

int  main(void)
{   
    char name[5];
    fgets(name, 4, stdin);    //fgets(name, 6, stdin);                                                                      
    puts(name);

    return 0;
}

编译运行:


如果num > strlem(name), 则函数最多接受strlen(name)个字符。


下面的代码是一个读取文件的例子:

/* fgets example */
#include 

int main()
{
   FILE * pFile;
   char mystring [100];

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else {
     if ( fgets (mystring , 100 , pFile) != NULL )
       puts (mystring);
     fclose (pFile);
   }
   return 0;
}


3. fputs函数

(1) 函数原型

int fputs ( const char * str, FILE * stream );

(2) 函数说明

Writes the string pointed by str to the stream.
The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to the stream.


(3) 参数

str An array containing the null-terminated sequence of characters to be written. stream Pointer to a FILE object that identifies the stream where the string is to be written.

(4) 返回值

On success, a non-negative value is returned.
On error, the function returns EOF.


如下代码

#include                                                                                   

int main(void) 
{   
    char name[5] = "1234";
    fputs(name, stdout);
    return 0;
}

输出:


fputs末尾不自动加入换行符。


下面的代码是与文件有关的一个例子:

/* fputs example */
#include 

int main ()
{
   FILE * pFile;
   char sentence [256];

   printf ("Enter sentence to append: ");
   fgets (sentence,255,stdin);
   pFile = fopen ("mylog.txt","a");
   fputs (sentence,pFile);
   fclose (pFile);
   return 0;
}


你可能感兴趣的:(技术文章)