scanf函数IStream对象上 [英] scanf on an istream object

查看:151
本文介绍了scanf函数IStream对象上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:我见过的帖子<一个href=\"http://stackoverflow.com/questions/14330637/what-is-the-cin-analougus-of-scanf-formatted-input\">What是scanf的格式化输入的CIN analougus?提出的问题和后在这里不解决我的问题了。该职位旨在为C ++ - 的方式来做到这一点,但正如我前面提到的,不方便只使用C ++ - 的方式有时做到这一点,我有这明显的例子

NOTE: I've seen the post What is the cin analougus of scanf formatted input? before asking the question and the post doesn't solve my problem here. The post seeks for C++-way to do it, but as I mentioned already, it is inconvenient to just use C++-way to do it sometimes and I have clear examples for that.

我想从IStream对象读取数据,有时候不方便只使用C ++ - 风格方面,如运营商>>,例如该数据是特殊形式123:456所以你要灌输,使':':要读作串并转换,或00123为空(在scanf中的%D,它是非常哈克,而非%D)小数代替八进制(相对于在scanf的%D),以及可能的其他许多情况

I am trying to read data from an istream object, and sometimes it is inconvenient to just use C++-style ways such as operator>>, e.g. the data are in special form 123:456 so you have to imbue to make ':' as space (which is very hacky, as opposed to %d:%d in scanf), or 00123 where you want to read as string and convert decimal instead of octal (as opposed to %d in scanf), and possibly many other cases.

我选择istream的作为接口的原因是因为它可以产生的,因此更加灵活。例如,我们可以创建内存流,或者实时生成一些定制流等C语言风格的FILE *,在另一方面,是非常有限的,至少在一个符合标准的方式,在创建自定义的流。

The reason I chose istream as interface is because it can be derived and therefore more flexible. For example, we can create in-memory streams, or some customized streams that generated on the fly, etc. C-style FILE*, on the other hand, is very limited, at least in a standard-compliant way, on creating customized streams.

所以我的问题是,有没有办法做到IStream对象上类似scanf数据提取?我想的fscanf读取内部逐个字符使用龟etc FILE *,同时也的IStream提供这样的接口。因此,它是直接复制粘贴的fscanf的code和替换文件*与istream对象可能的,但是这是非常哈克。是否有一个更聪明和更清洁的方式,或者是有一些现有的工作对这个?

So my questions is, is there a way to do scanf-like data extraction on istream object? I think fscanf internally read character by character from FILE* using fgetc, while istream also provides such interface. So it is possible by just copying and pasting the code of fscanf and replace the FILE* with the istream object, but that's very hacky. Is there a smarter and cleaner way, or is there some existing work on this?

感谢。

推荐答案

根据@ tmyklebu的评论,我实现了streamScanf它通过fopencookie的包装istream的为FILE *:的 https://github.com/likan999/$c$cjam/blob/master/Common/StreamScanf.cpp

Based on @tmyklebu's comment, I implemented streamScanf which wraps istream as FILE* via fopencookie: https://github.com/likan999/codejam/blob/master/Common/StreamScanf.cpp

这篇关于scanf函数IStream对象上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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