When should I use fputs instead of fprintf?
What exactly is the difference between the two?
fprintf does formatted output. That is, it reads and interprets a format string that you supply and writes to the output stream the results.
fputs simply writes the string you supply it to the indicated output stream.
fputs()
doesn't have to parse the input string to figure out that all you want to do is print a string.fprintf()
allows you to format at the time of outputting.
As have been pointed out by other commenters (and as it's obvious from the docs) the great difference is that printf
allows formatting of arguments.
Perhaps you are asking if the functions are equivalent where no additional arguments are passed to printf()
? Well, they are not.
char * str;
FILE * stream;
...
fputs(str,stream); // this is NOT the same as the following line
fprintf(stream,str); // this is probably wrong
The second is probably wrong, because the string argument to fprintf()
is a still a formating string: if it has a '%' character it will be interpreted as a formatting specifier.
The functionally equivalent (but less direct/efficient/nice) form would be
fprintf(stream,"%s", str);
Uhm... ...puts()
just writes a string, while printf()
has a number of formatting facilities for several types of data.
fputs()
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/
fprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/
Documentation is useful! Learn to read it, and you'll have a powerful tool on your side.
ReferenceURL : https://stackoverflow.com/questions/5690979/when-should-i-use-fputs-instead-of-fprintf
'programing' 카테고리의 다른 글
유형 오류: 어레이에서 속성 1을 추가할 수 없습니다. 개체를 확장할 수 없습니다.푸시() (0) | 2023.06.19 |
---|---|
SQL Server 인스턴스의 데이터 디렉토리를 찾는 방법은 무엇입니까? (0) | 2023.06.19 |
CASE 및 COALESCE 단락 평가는 PL/SQL의 시퀀스에서는 작동하지만 SQL에서는 작동하지 않습니다. (0) | 2023.06.19 |
노드 JS에서 셀러리와 동등한 값 (0) | 2023.06.19 |
Python의 열린 파일에서 경로 가져오기 (0) | 2023.06.19 |