C: fwrite() vs (f)printf?

First User

I was reading the manual page for the getline function and saw a demonstration of it :

 #define _GNU_SOURCE
       #include <stdio.h>
       #include <stdlib.h>

       int
       main(int argc, char *argv[])
       {
           FILE *stream;
           char *line = NULL;
           size_t len = 0;
           ssize_t nread;

        ...
           while ((nread = getline(&line, &len, stream)) != -1) {
               printf("Retrieved line of length %zu:\n", nread);
               fwrite(line, nread, 1, stdout); /* ? */
           }

           free(line);
           fclose(stream);
           exit(EXIT_SUCCESS);
       }

I replaced the fwrite() statement with printf ("%s", line) and it produced identical results (compared using cmp and diff). I am aware of the distinction between fwrite and fprint but was there any specifc reason the author chose to use fwrite() over fprintf or printf in this context ?

chux - Reinstate Monica

Difference between fwrite(line, nread, 1, stdout) and printf ("%s", line) includes:

printf ("%s", line) writes up to the 1st null character.

fwrite(line, nread, 1, stdout) writes to length of input.

This differs when a null character was read and so using fwrite() provides correct functionality in that pathological case.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Fprintf VS _ftprintf

分類Dev

C:fwrite()と(f)printf?

分類Dev

cでのfprintとfprintf

分類Dev

How to perform fwrite in a loop in c++

分類Dev

python struct.pack and write vs matlab fwrite

分類Dev

C:CtrlCで中断されたfprintf

分類Dev

Why fwrite doesn't work for binary files in C?

分類Dev

警告fprintf

分類Dev

Cでfscanfとfprintfを一緒に使用する

分類Dev

c ++ fprintfは、最初の文字列のみを出力します

分類Dev

エラーのあるfprintfと同等のC ++

分類Dev

C fopen / fwriteが私を狂気に駆り立てる-fwriteが書いていない

分類Dev

C fwrite()がCPUを100%でスタックしました

分類Dev

C ++でループ内でfwriteを実行する方法

分類Dev

C ++ fwrite、エラーコード36 Broken Pipie

分類Dev

fwrite()は、書き込みCの代わりに追加します

分類Dev

Cのfwriteとstructの奇妙なエラー

分類Dev

C / C ++ `!a` vs` a == 0`

分類Dev

fprintf is not printing to the file

分類Dev

Passing Varargin into fprintf

分類Dev

fprintf - specifying path

分類Dev

Matlab: output format with fprintf

分類Dev

fprintf outputting ')' in txt file

分類Dev

Dev-C ++のfprintfでのセグメンテーション違反

分類Dev

fscanfとfprintfはCでバッファリングされていますか?

分類Dev

C:出力ファイルに出力するfprintfを取得できません

分類Dev

cコードで電子メールにパイプをfwriteする

分類Dev

Cのfwrite()およびfread()パラメーターに関する混乱

分類Dev

Cはファイルから情報をロードしません(fread、fwrite)

Related 関連記事

ホットタグ

アーカイブ