重定向输出遇到的缓冲问题 .

转自:http://blog.csdn.net/silyvin/article/details/8281342

一个项目中需要迭代计算,时间长,但是在重定向输出的时候所有标准输出不能及时输出,这就要求程序主函数正常运行完后才能正常输出到文件。

因为标准输出到终端时默认行缓冲或无缓冲,重定向到硬盘之后,就变成了全缓冲


1. Fully buffered means that I/O takes place only when the buffer is fully, the process explicitly calls fflush, or the process terminates by calling exit. A common size for the standard I/O buffer is 8192 bytes;
 
2. Line buffered means that I/O takes place when a newline is encountered, when the process calls fflush, or when the process terminates by calling exit.
 
3. Unbuffered means that I/O take place each time a standard I/O output function is called.
 

解决方法有 fflush  , setvbuf  ,  和伪终端

1. 每次printf时都刷新缓存区强制输出,fflush(stdout)

2. setvbuf 更改缓冲类型,手动设置缓冲区大小,使之足够小。

int setvbuf( FILE *stream, char *buffer, intmode, size_tsize );

The setvbuf function allows the program to control both buffering and buffer size forstream.stream must refer to an open file that has not undergone an I/O operation since it was opened. The array pointed to bybuffer is used as the buffer, unless it isNULL, in which casesetvbuf uses an automatically allocated buffer of lengthsize/2 * 2 bytes.

The mode must be _IOFBF, _IOLBF, or _IONBF. If mode is _IOFBF or _IOLBF, then size is used as the size of the buffer. If mode is_IONBF, the stream is unbuffered andsize and buffer are ignored. Values formode and their meanings are:

_IOFBF

Full buffering; that is, buffer is used as the buffer andsize is used as the size of the buffer. Ifbuffer is NULL, an automatically allocated buffersize bytes long is used.

_IOLBF

With MS-DOS, the same as _IOFBF.

_IONBF

No buffer is used, regardless of buffer or size.

对于 _IOFBF _IOLBF ,dos上并没有区别,第四个参数size指定了缓冲区的大小,并且,当第二个参数,一个字符串buffer没有指定的情况下,系统将自动分配一片内存,长度为 (unsigned int)(size/2) * 2  ,每次向缓冲区写size个,大于缓冲区大小后,自动输出。


测试:

x.cpp

  1. #include <stdio.h>   
  2. #include <windows.h>   
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     setvbuf(stdout, 0, _IOFBF, <SPAN style="COLOR: #ff0000">10</SPAN>);  
  7.     printf("hello world\n");  
  8.     Sleep(10000000);  
  9.       
  10.     return 0;  
  11. }  
#include <stdio.h>
#include <windows.h>

int main(int argc, char *argv[])
{
	setvbuf(stdout, 0, _IOFBF, 10);
	printf("hello world\n");
	Sleep(10000000);
	
	return 0;
}


cl x.cpp
x > re.txt
pause

运行后直接退出

输出 hello worl


将10改成14后没有输出

改成4后输出 hello wo


在这个项目中 setvbuf(stdout, 0, _IONBF, 0);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在STM32F407系列微控制器中,printf是一个常用的调试输出函数,通常用于打印变量值、字符串等信息。然而,由于STM32的硬件资源有限,特别是没有集成标准的UART或USART,直接使用printf可能会遇到问题。为了解决这个限制,开发者通常会选择重定向printf输出到其他可用的通信接口,如HAL库提供的USB CDC、UART、I2C或SPI。 1. **重定向到USB CDC(通用串行控制器)**:如果你的项目支持USB调试,可以通过配置USB设备为虚拟COM口,然后在HAL库中设置相应的USB CDC驱动,使其成为printf的输出目的地。 ```c HAL_UART_Init(&huart1); // 初始化一个用于printf的串口 hal_UART_SetCallback(&huart1, &usart_rx HAL_UART_Callback); // 设置接收数据的回调函数 ``` 2. **重定向到HAL UART**:如果选择传统的UART,你可以先初始化一个HAL UART结构体,然后注册一个回调函数来捕获打印信息并发送到指定的UART端口。 ```c void usart_rx(UART_HandleTypeDef *huart, uint8_t Data) { // 捕获并处理printf格式的数据 if (Data == '\n') { // 发送换行符到UART HAL_UART_Transmit(huart, "\r", 1, HAL_MAX_DELAY); } else { // 发送其他字符 HAL_UART_Transmit(huart, &Data, 1, HAL_MAX_DELAY); } } ``` 3. **使用HAL I/O Stream Interface(IStream)**:一些第三方库如mbed或FreeRTOS可能会提供一个I/O抽象层,你可以将printf连接到这个层,然后由该层决定如何将数据发送出去。 在进行重定向时,确保正确初始化通信接口,并且处理好数据格式的解析和传输延迟。此外,可能还需要考虑printf的缓冲机制,确保数据能够被完整且及时地发送出去。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值