Home > Common Problem > body text

The difference between fprintf and printf

zbt
Release: 2023-11-28 10:48:49
Original
2093 people have browsed it

The difference between fprintf and printf is that the output destination is different. printf outputs to the standard output stream, while fprintf outputs to the specified file stream. Select the appropriate function to perform output operations as needed. It should be noted that the fprintf function needs to open the file through the fopen function first, and close the file through the fclose function after use. In addition, if the file opening fails or an operation error occurs, error handling is required.

The difference between fprintf and printf

fprintf and printf are output functions in C language. The difference between them lies in the different output targets.

The printf function is used to output formatted data to the standard output stream stdout, usually displayed on the terminal. Its usage is as follows:

int printf(const char *format, ...)
Copy after login

fprintf function is used to output formatted data to the specified file stream. Its usage is as follows:

int fprintf(FILE *stream, const char *format, ...)
Copy after login

The parameters and format control strings of both are basically used in the same way, and both output data according to the specified format. The only difference is that printf outputs to the standard output stream stdout, while fprintf outputs to the specified file stream.

The following is a simple example demonstrating the use of printf and fprintf functions:

#include
int main() {
FILE *file;
// 打开文件
file = fopen("output.txt", "w");
// 使用printf输出到标准输出流
printf("Hello, World!\n");
// 使用fprintf输出到文件流
fprintf(file, "Hello, World!\n");
// 关闭文件
fclose(file);
return 0;
}
Copy after login

In the above example, we use the printf function to replace "Hello, World!" is output to the standard output stream, and the same content is output to a file named "output.txt" through the fprintf function. The output of the printf function will be displayed on the terminal, and the output of the fprintf function will be written to the file .

It should be noted that the fprintf function needs to first open the file through the fopen function, and close the file through the fclose function after use. In addition, if the file opening fails or an operation error occurs, error handling is required.

In summary, the printf and fprintf functions are both functions used to output formatted data. The difference lies in the different output targets. printf outputs to the standard output stream, while fprintf outputs to the specified file stream. Choose the appropriate one according to your needs. function to perform output operations.

The above is the detailed content of The difference between fprintf and printf. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template