c语言printf和flush用法,C中printf 和write的区别

调试程序过程中遇到一个问题:遇到printf的语句时有时候能马上打印出字符串,有时候要程序退出时才一次性打印出字符串,但是write每次都能直接打印出字符串。

原来是因为printf是行缓冲函数,只有满了一行才马上打印, write在用户态没有缓冲,所以直接输出。

eg:

#include #include #include int main(void)

{

int fd;

char str[100];

//创建文件,并输入任意内容供测试

fd = open("test3.6.file", O_WRONLY | O_CREAT, 0660);

if(fd < 0)

err_quit("open test3.6.file failed");

write(fd, "Hello world!\n", 13);

write(fd, "1234567890\n", 11);

write(fd, "abcdefg\n", 8);

close(fd);

//测试读取从 文件开头,中间,结尾

fd = open("test3.6.file", O_RDWR | O_APPEND);

if(fd < 0)

err_quit("open test3.6.file rdwr failed");

printf("read from start: ");

//fflush(stdout);

lseek(fd, 0, SEEK_SET);

if( read(fd, str, 13) > 0)

write(STDOUT_FILENO, str, strlen(str));

printf("read from MID: ");

// fflush(stdout);

lseek(fd, 14, SEEK_SET);

if( read(fd, str, 11) > 0)

write(STDOUT_FILENO, str, strlen(str));

printf("read from END: ");

// fflush(stdout);

lseek(fd, -8, SEEK_END);

if( read(fd, str, 8) > 0)

write(STDOUT_FILENO, str, strlen(str));

//测试往文件中写

lseek(fd, 0, SEEK_SET);

write(fd, "##########", 10);

lseek(fd, 20, SEEK_SET);

write(fd, "@@@@@@@@@@", 10);

lseek(fd, 0, SEEK_END);

write(fd, "**********", 10);

exit(0);

}

如果把上面代码中fflush注释掉,那每次运行的结果如下:

Hello world!

234567890

********

read from start: read from MID: read from END:

如果再printf打印的字符串后面添加‘\n’或者fflush, 则能正常输出:

read from start: Hello world!

read from MID: 234567890

read from END: ********

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言没有内置的Base64加解密函数,但可以通过调用第三方库来实现。 一个常用的第三方库是openssl,使用它可以很方便地进行Base64编码和解码。 以下是一个示例程序,展示了如何使用openssl库进行Base64编码和解码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/bio.h> #include <openssl/evp.h> int base64_encode(const char *input, int input_len, char *output, int output_size) { BIO *bmem = NULL; BIO *b64 = NULL; BUF_MEM *bptr = NULL; b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return -1; } bmem = BIO_new(BIO_s_mem()); if (bmem == NULL) { BIO_free(b64); return -1; } b64 = BIO_push(b64, bmem); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); if (BIO_write(b64, input, input_len) <= 0) { BIO_free_all(b64); return -1; } if (BIO_flush(b64) <= 0) { BIO_free_all(b64); return -1; } BIO_get_mem_ptr(b64, &bptr); if (bptr->length > output_size) { BIO_free_all(b64); return -1; } memcpy(output, bptr->data, bptr->length); output[bptr->length] = '\0'; BIO_free_all(b64); return bptr->length; } int base64_decode(const char *input, int input_len, char *output, int output_size) { BIO *bmem = NULL; BIO *b64 = NULL; b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return -1; } bmem = BIO_new_mem_buf((void *)input, input_len); if (bmem == NULL) { BIO_free(b64); return -1; } bmem = BIO_push(b64, bmem); int len = BIO_read(bmem, output, output_size); if (len < 0) { BIO_free_all(bmem); return -1; } output[len] = '\0'; BIO_free_all(bmem); return len; } int main() { char input[] = "hello world"; char encoded[100]; char decoded[100]; printf("Original: %s\n", input); int len = base64_encode(input, strlen(input), encoded, sizeof(encoded)); printf("Encoded: %s (%d)\n", encoded, len); len = base64_decode(encoded, len, decoded, sizeof(decoded)); printf("Decoded: %s (%d)\n", decoded, len); return 0; } ``` 运行结果如下: ``` Original: hello world Encoded: aGVsbG8gd29ybGQ= (16) Decoded: hello world (11) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值