[英]What is the difference between sprintf and printf for microcontrollers
[英]Difference between fprintf, printf and sprintf?
任何人都可以用簡單的英語解釋printf
、 fprintf
和sprintf
之間的區別嗎?
它在什么 stream 中?
在閱讀“C 中的文件處理”時,我真的對這三者感到困惑。
在 C 中,“流”是一種抽象; 從程序的角度來看,它只是字節的生產者(輸入流)或消費者(輸出流)。 它可以對應於磁盤上的文件、管道、終端或其他一些設備,例如打印機或 tty。 FILE
類型包含有關流的信息。 通常,您不會直接處理FILE
對象的內容,只需將指向它的指針傳遞給各種 I/O 例程即可。
一共有三個標准流: stdin
是指向標准輸入流的指針, stdout
是指向標准輸出流的指針, stderr
是指向標准錯誤輸出流的指針。 在交互式會話中,這三個通常指的是您的控制台,盡管您可以將它們重定向到指向其他文件或設備:
$ myprog < inputfile.dat > output.txt 2> errors.txt
在這個例子中, stdin
現在指向inputfile.dat
, stdout
指向output.txt
, stderr
指向errors.txt
。
fprintf
將格式化文本寫入您指定的輸出流。
printf
相當於寫入fprintf(stdout, ...)
並將格式化文本寫入標准輸出流當前指向的任何位置。
sprintf
將格式化文本寫入char
數組,而不是流。
printf
輸出到標准輸出流 ( stdout
)
fprintf
轉到文件句柄( FILE*
)
sprintf
轉到您分配的緩沖區。 ( char*
)
printf(const char *format, ...)
用於將數據打印到通常是計算機顯示器的標准輸出上。sprintf(char *str, const char *format, ...)
就像printf
。 它不是在標准輸出(即監視器)上顯示格式化的字符串,而是將格式化的數據存儲在由 char 指針(第一個參數)指向的字符串中。 字符串位置是 printf 和 sprint 語法之間的唯一區別。fprintf(FILE *stream, const char *format, ...)
再次類似於printf
。 在這里,不是在監視器上顯示數據,或者將其保存在某個字符串中,而是將格式化的數據保存在文件指針指向的文件中,該文件指針用作fprintf
的第一個參數。 文件指針是printf
語法的唯一補充。 如果stdout
文件用作fprintf
中的第一個參數,則其工作被認為等同於printf
的工作。
printf(...)
等價於fprintf(stdout,...)
。
fprintf
用於輸出到流。
sprintf(buffer,...)
用於將字符串格式化為緩沖區。
注意還有vsprintf
、 vfprintf
和vprintf
打印
printf("control string ", argument );
fprintf
fprintf (filename, "control string ", argument );
您還可以使用 vsnprintf() 函數做一些非常有用的事情:
$ cat test.cc
#include <exception>
#include <stdarg.h>
#include <stdio.h>
struct exception_fmt : std::exception
{
exception_fmt(char const* fmt, ...) __attribute__ ((format(printf,2,3)));
char const* what() const throw() { return msg_; }
char msg_[0x800];
};
exception_fmt::exception_fmt(char const* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(msg_, sizeof msg_, fmt, ap);
va_end(ap);
}
int main(int ac, char** av)
{
throw exception_fmt("%s: bad number of arguments %d", *av, ac);
}
$ g++ -Wall -o test test.cc
$ ./test
terminate called after throwing an instance of 'exception_fmt'
what(): ./test: bad number of arguments 1
Aborted (core dumped)
sprintf:將格式化數據寫入內存中的字符串而不是標准輸出
sprintf 的語法是:
#include <stdio.h>
int sprintf (char *string, const char *format
[,item [,item]…]);
這里,
String 是指向內存中要寫入數據的緩沖區的指針。
格式是指向定義格式的字符串的指針。
每個項目都是一個變量或表達式,指定要寫入的數據。
如果操作成功,則 sprintf 返回的值大於或等於零,換句話說,寫入的字符數(不包括終止的空字符)將返回,如果發生錯誤,則返回小於零的值。
printf:打印到標准輸出
printf 的語法是:
printf format [argument]…
sprintf() 和 printf() 之間的唯一區別是 sprintf() 將數據寫入字符數組,而 printf() 將數據寫入標准輸出設備 stdout。
fprintf
這與流有關,其中printf
是類似於fprintf
但與流無關的語句,即fprintf
與文件相關
其他人已經提供了詳細的解釋; 我將通過一個非常基本的示例將我的答案限制在關於print
與sprintf
的實際討論上。
假設您希望您的程序同時具有 output、當前行號和文件名。 具體來說,您希望:(i) 將其打印在屏幕上,(ii) 將其保存在變量中,以備將來使用。 您可以將printf
用於 (i),將sprintf
用於 (ii)。 這是代碼。
/* saves file name and current line in a string and prints it on the screen*/
#include <stdio.h>
int main(void) {
/* note the use of a marco to save the line nr. */
int line_n= __LINE__;
/* note the use of a marco to save the file name */
char file_name[]= __FILE__;
/* Some text you wish to print/save */
char line[] = "Line ";
char file[]= " of file ";
char my_str[100];
/* expand everything and save it in my_str for future use */
sprintf(my_str, "%s%d%s%s", line, line_n, file, file_name);
/* or just print it out on the screen */
printf("%s", my_str);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.