C时的读数(从标准输入)停止在性格0X1A [英] C reading (from stdin) stops at 0x1a character
问题描述
我目前正在实施Burrows-Wheeler变换(以及反变换)的原始数据(如JPG等)。
当像TEXTFILES正常数据测试没有发生问题。但是,当涉及到阅读JPG文件例如它在人物停止读取0X1A又名替代字符。
我一直在通过互联网寻找这并不需要OS dependend code解决方案,但没有结果...
我想在二进制模式标准输入读取,但不是很容易的,我猜。有没有办法解决这个问题的简单的方法?
code:
=缓冲(无符号字符*)释放calloc(BLOCK_SIZE + 1的sizeof(无符号字符));
长度= FREAD((无符号字符*)缓冲区,1,BLOCK_SIZE,标准输入);
如果(长度== 0){
//文件是空的
}其他{
b_length =长度;
而(长度== b_length){
缓冲[BLOCK_SIZE] ='\\ 0';
EN codeBLOCK(缓冲区长度);
长度= FREAD((无符号字符*)缓冲区,1,BLOCK_SIZE,标准输入);
}
如果(长度!= 0){
缓冲区[长度] ='\\ 0';
EN codeBLOCK(缓冲区长度);
}
}
免费(缓冲);
当你已经注意到了,你从标准输入
阅读ASCII模式,它是击中SUB字符(替代品,又名<大骨节病> CTRL 骨节病> + <大骨节病>以Z 骨节病>,又名DOS档案结尾)。
您必须改变模式, setmode为二进制
而在Windows上:
#如果定义(WIN32)
#包括LT&;&io.h GT;
#包括LT&;&fcntl.h GT;
#ENDIF / *定义(WIN32)* // * ... * /#如果定义(WIN32)
_setmode(_fileno(标准输入),_O_BINARY);
#ENDIF / *定义(WIN32)* /
在Windows以外的平台上,你不会遇到这种区别的模式。
currently I'm implementing the Burrows-Wheeler transform (and inverse transform) for raw data (like jpg etc.). When testing on normal data like textfiles no problems occur. But when it comes to reading jpg files e.g. it stops reading at character 0x1a aka substitute character. I've been searching through the internet for solutions which doesn't take OS dependend code but without results... I was thinking to read in stdin in binary mode but that isn't quite easy I guess. Is there any simple method to solve this problem?
code:
buffer = (unsigned char*) calloc(block_size+1,sizeof(unsigned char));
length = fread((unsigned char*) buffer, 1, block_size, stdin);
if(length == 0){
// file is empty
}else{
b_length = length;
while(length == b_length){
buffer[block_size] = '\0';
encodeBlock(buffer,length);
length = fread((unsigned char*) buffer, 1, block_size, stdin);
}
if(length != 0){
buffer[length] = '\0';
encodeBlock(buffer,length);
}
}
free(buffer);
As you've noticed, you're reading from stdin
in ASCII mode and it is hitting the SUB character (substitute, aka CTRL+Z, aka DOS End-of-File).
You have to change the mode to binary with setmode
while on Windows:
#if defined(WIN32)
#include <io.h>
#include <fcntl.h>
#endif /* defined(WIN32) */
/* ... */
#if defined(WIN32)
_setmode(_fileno(stdin), _O_BINARY);
#endif /* defined(WIN32) */
On platforms other than Windows you don't run into this distinction in modes.
这篇关于C时的读数(从标准输入)停止在性格0X1A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!