c语言中字符串函数原型,C语言函数原型fgets fputs

fputs

int fputs ( const char * str, FILE * stream

);

Write string to stream

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.

Parameters

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.

Return Value

On success, a non-negative value

is returned.

On error, the function returns EOF.

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;

}

This program allows to append a

line to a file called mylog.txt each time it is run.

fgets

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

Get string from

stream

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 a

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.

Parameters

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.

Return Value

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.

Use either ferror or feof to

check whether an error happened or the End-of-File was

reached.

Example

#include

int main()

{

FILE * pFile;

char mystring [100];

pFile = fopen ("myfile.txt" , "r");

if (pFile == NULL) perror ("Error opening file");

else {

fgets (mystring , 100 , pFile);

puts (mystring);

fclose (pFile);

}

return 0;

}

This example reads the first line

of myfile.txt or the first 100 characters, whichever comes

first, and prints them on the screen.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值