C 读取(从标准输入)在 0x1a 字符处停止 [英] C reading (from stdin) stops at 0x1a character

查看:28
本文介绍了C 读取(从标准输入)在 0x1a 字符处停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在为原始数据(如 jpg 等)实施 Burrows-Wheeler 变换(和逆变换).在对文本文件等普通数据进行测试时,不会出现问题.但是在阅读 jpg 文件时,例如它在字符 0x1a 又名替代字符处停止读取.我一直在通过互联网搜索不采用操作系统相关代码但没有结果的解决方案......我想以二进制模式读取标准输入,但我猜这并不容易.有什么简单的方法可以解决这个问题吗?

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?

代码:

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] = '';
        encodeBlock(buffer,length);
        length = fread((unsigned char*) buffer, 1, block_size, stdin);      
    }
    if(length != 0){            
        buffer[length] = '';
        encodeBlock(buffer,length);
    }
}
free(buffer);

推荐答案

正如您所注意到的,您正在以 ASCII 模式从 stdin 中读取,并且遇到了 SUB 字符(替代,又名CTRL+Z,也就是 DOS 文件结束符).

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).

您必须使用 setmode 在 Windows 上:

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) */

在 Windows 以外的平台上,您不会遇到这种模式差异.

On platforms other than Windows you don't run into this distinction in modes.

这篇关于C 读取(从标准输入)在 0x1a 字符处停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆