fprintf() 举个例子
时间: 2023-05-19 07:03:05 浏览: 77
fprintf() 是一个 C 语言中的函数,用于将格式化的数据输出到指定的文件中。它的语法如下:
int fprintf(FILE *stream, const char *format, ...);
其中,stream 是一个指向 FILE 结构体的指针,用于指定输出的文件;format 是一个格式化字符串,用于指定输出的格式;... 表示可变参数列表,用于指定输出的数据。举个例子,下面的代码将会把字符串 "Hello, world!" 输出到标准输出流中:
fprintf(stdout, "Hello, world!\n");
相关问题
写个c程序举个例子
好的,以下是一个简单的 C 语言程序,用于获取 DRM 设备的当前帧缓冲对象并将其保存为 BMP 图像文件:
```c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <linux/drm.h>
#include <drm/drm_fourcc.h>
#define WIDTH 800
#define HEIGHT 600
int main(int argc, char **argv)
{
// 打开 DRM 设备
int fd = open("/dev/dri/card0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "failed to open DRM device\n");
exit(EXIT_FAILURE);
}
// 获取当前显示的帧缓冲对象
drmModeRes *resources = drmModeGetResources(fd);
if (!resources) {
fprintf(stderr, "failed to get DRM resources\n");
close(fd);
exit(EXIT_FAILURE);
}
drmModeConnector *connector = NULL;
drmModeEncoder *encoder = NULL;
drmModeCrtc *crtc = NULL;
for (int i = 0; i < resources->count_connectors; i++) {
connector = drmModeGetConnector(fd, resources->connectors[i]);
if (connector->connection == DRM_MODE_CONNECTED) {
for (int j = 0; j < connector->count_encoders; j++) {
encoder = drmModeGetEncoder(fd, connector->encoders[j]);
if (encoder->crtc_id) {
crtc = drmModeGetCrtc(fd, encoder->crtc_id);
break;
}
drmModeFreeEncoder(encoder);
}
}
drmModeFreeConnector(connector);
if (crtc) break;
}
if (!crtc) {
fprintf(stderr, "failed to get current CRTC\n");
drmModeFreeResources(resources);
close(fd);
exit(EXIT_FAILURE);
}
// 获取当前帧缓冲对象的信息
drmModeFB *fb = drmModeGetFB(fd, crtc->buffer_id);
if (!fb) {
fprintf(stderr, "failed to get current FB\n");
drmModeFreeCrtc(crtc);
drmModeFreeResources(resources);
close(fd);
exit(EXIT_FAILURE);
}
if (fb->width != WIDTH || fb->height != HEIGHT) {
fprintf(stderr, "current FB size is not %dx%d\n", WIDTH, HEIGHT);
drmModeFreeFB(fb);
drmModeFreeCrtc(crtc);
drmModeFreeResources(resources);
close(fd);
exit(EXIT_FAILURE);
}
// 映射帧缓冲对象到用户空间
uint32_t *buf = NULL;
struct drm_mode_map_dumb map_dumb = {
.handle = fb->handle,
};
if (ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb) < 0) {
fprintf(stderr, "failed to map dumb buffer\n");
drmModeFreeFB(fb);
drmModeFreeCrtc(crtc);
drmModeFreeResources(resources);
close(fd);
exit(EXIT_FAILURE);
}
buf = mmap(NULL, fb->size, PROT_READ, MAP_SHARED, fd, map_dumb.offset);
if (buf == MAP_FAILED) {
fprintf(stderr, "failed to mmap dumb buffer\n");
drmModeFreeFB(fb);
drmModeFreeCrtc(crtc);
drmModeFreeResources(resources);
close(fd);
exit(EXIT_FAILURE);
}
// 转换像素格式为 RGB565
uint16_t *rgb_buf = (uint16_t *)malloc(WIDTH * HEIGHT * sizeof(uint16_t));
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
uint32_t pixel = buf[i * fb->pitch / 4 + j];
uint16_t r = (pixel >> 16) & 0xff;
uint16_t g = (pixel >> 8) & 0xff;
uint16_t b = pixel & 0xff;
rgb_buf[i * WIDTH + j] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
}
}
// 保存为 BMP 图像文件
FILE *fp = fopen("drm_fb.bmp", "wb");
if (fp) {
uint8_t bmp_header[54] = {
0x42, 0x4d, 0x36, 0x20, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x20, 0x03,
0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x0c, 0x00,
0x00, 0x13, 0x0b, 0x00, 0x00, 0x13, 0x0b, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
};
uint32_t row_size = WIDTH * 2;
uint32_t file_size = sizeof(bmp_header) + row_size * HEIGHT;
uint32_t padding_size = (4 - (row_size % 4)) % 4;
uint8_t padding[4] = {0};
bmp_header[2] = file_size;
bmp_header[6] = file_size >> 8;
bmp_header[10] = sizeof(bmp_header);
bmp_header[18] = WIDTH;
bmp_header[19] = WIDTH >> 8;
bmp_header[20] = WIDTH >> 16;
bmp_header[21] = WIDTH >> 24;
bmp_header[22] = HEIGHT;
bmp_header[23] = HEIGHT >> 8;
bmp_header[24] = HEIGHT >> 16;
bmp_header[25] = HEIGHT >> 24;
fwrite(bmp_header, 1, sizeof(bmp_header), fp);
for (int i = 0; i < HEIGHT; i++) {
fwrite(rgb_buf + (HEIGHT - i - 1) * WIDTH, 1, row_size, fp);
fwrite(padding, 1, padding_size, fp);
}
fclose(fp);
}
// 释放资源
free(rgb_buf);
munmap(buf, fb->size);
drmModeFreeFB(fb);
drmModeFreeCrtc(crtc);
drmModeFreeResources(resources);
close(fd);
return 0;
}
```
注意,该程序只是一个简单的示例,实际应用中还需要考虑各种异常情况的处理。
举个例子,这个函数怎么使用
好的,这里给一个简单的例子来说明如何使用`fdt_setprop`函数。假设我们要给Device Tree中的一个节点`/chosen`设置一个名为`bootargs`的属性,并将其值设置为字符串`"console=ttyS0,115200"`。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <libfdt.h>
int main(int argc, char **argv)
{
int fd, err;
void *dtb;
const char *path = "/chosen";
const char *propname = "bootargs";
const char *propval = "console=ttyS0,115200\0";
// 打开Device Tree文件并将其映射到内存中
fd = open("/proc/device-tree/dtb", O_RDONLY);
if (fd < 0) {
perror("open");
return -1;
}
dtb = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
if (dtb == MAP_FAILED) {
perror("mmap");
close(fd);
return -1;
}
// 找到节点并设置属性
err = fdt_setprop(dtb, fdt_path_offset(dtb, path), propname, propval, strlen(propval) + 1);
if (err) {
fprintf(stderr, "fdt_setprop failed with error %d\n", err);
munmap(dtb, 4096);
close(fd);
return -1;
}
// 保存修改后的Device Tree文件
err = fdt_pack(dtb);
if (err) {
fprintf(stderr, "fdt_pack failed with error %d\n", err);
munmap(dtb, 4096);
close(fd);
return -1;
}
// 关闭文件和内存映射
munmap(dtb, 4096);
close(fd);
return 0;
}
```
这个例子中,我们首先打开了Device Tree文件`/proc/device-tree/dtb`并将其映射到内存中。然后,通过`fdt_path_offset`函数找到了节点`/chosen`的偏移量,并使用`fdt_setprop`函数设置了它的属性`bootargs`的值为`"console=ttyS0,115200"`。最后,使用`fdt_pack`函数保存修改后的Device Tree文件。值得注意的是,在设置属性值时,需要将字符串的长度加上1,以包含字符串末尾的空字符`\0`。