fputs ()- 寫入字串
格式
#include <stdio.h>
int fputs(const char *string, FILE *stream);語言層次
ANSI
安全執行緒
是
說明
fputs() 函數會將 字串 複製到現行位置的輸出 串流 。 它不會複製字串結尾的空值字元 (\0)。
回覆值
如果發生錯誤, fputs() 函數會傳回 EOF ; 否則會傳回非負值。
以 type=record開啟的檔案不支援 fputs() 函數。
如需 fputs()的錯誤碼值的相關資訊,請參閱 fputc ()-寫入字元。
範例
此範例將字串寫入串流。
#include <stdio.h>
#define NUM_ALPHA 26
int main(void)
{
FILE * stream;
int num;
/* Do not forget that the '\0' char occupies one character */
static char buffer[NUM_ALPHA + 1] = "abcdefghijklmnopqrstuvwxyz";
if ((stream = fopen("mylib/myfile", "w")) != NULL )
{
/* Put buffer into file */
if ( (num = fputs( buffer, stream )) != EOF )
{
/* Note that fputs() does not copy the \0 character */
printf( "Total number of characters written to file = %i\n", num );
fclose( stream );
}
else /* fputs failed */
perror( "fputs failed" );
}
else
perror( "Error opening myfile" );
}