开发者

How to write a full string to a file?

开发者 https://www.devze.com 2022-12-19 04:53 出处:网络
I\'m programming in Windows right now, but portable code would be welcomed too. What I\'m using right now is fwrite(4), but this function needs a maximum number of elements to be wri开发者_JAVA百科tt

I'm programming in Windows right now, but portable code would be welcomed too.

What I'm using right now is fwrite(4), but this function needs a maximum number of elements to be wri开发者_JAVA百科tten to the file. I can use strlen(1) here but I'd like to know if there is any better way to do this.


Use fputs instead:

FILE * f = fopen( "myfile.txt", "w" );
fputs( "Hello world\n", f );


You can use fputs, but using functions that require the size to be written are safer (buffer overrun).

So, IMHO, using fwrite would be the preferred choice.


You might check for a null within the expected length first.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号