我正在运行一本C++教科书,我将其作为C++编程的复习.其中一个练习问题(没有太多细节)希望我定义一个可以传递ifstream或cin(例如istream)作为参数的函数.从那里,我必须通读流.麻烦的是,我无法想出一个办法能有这个一个功能使用cin和ifstream的有效发现流的末尾.也就是说,
while(input_stream.peek() != EOF)
Run Code Online (Sandbox Code Playgroud)
不会为cin工作.我可以重新修改函数来查找某个短语(比如"#End of Stream#"等),但我认为如果我传递的文件流有这个确切的短语,这是一个坏主意.
我曾经想过要使用函数重载,但到目前为止,本书已经提到它何时需要我这样做.我可能在这个实践问题上投入了太多精力,但我喜欢这个创作过程并且好奇是否有这样的方法可以做到这一点而不会超载.
我有一个istream,我需要读取具体数量的字节,但我不知道它的长度.它被终止.我想我可以1)写一个循环并一次读取一个字节2)告诉它给我一个缓冲区或字符串,它现在开始直到某个字节(在这种情况下为0).或者3)一次只读取一个字节的buf并检查它是否为0,如果不是则将其附加到字符串.
第三个我知道我可以做,但其他2听起来像istream可能是可能的(在这种情况下它是一个文件流).我还在阅读istream的文档.有很多.
我试图从a读取行,std::istream但输入可能包含'\r' 和/或 '\n',所以std::getline没有用.
很抱歉大喊,但这似乎需要强调......
有没有标准的方法来做到这一点?目前我正在努力
char c;
while (in >> c && '\n' != c && '\r' != c)
out .push_back (c);
Run Code Online (Sandbox Code Playgroud)
......但这会跳过空白.D'哦!std::noskipws- 需要更多的摆弄,现在它是misehaving.
当然必须有更好的方法吗?!?
我有一个不规则的列表,其中的数据如下所示:
[Number] [Number]
[Number] [Number] [Number]
[Number] [Number] [Number]
[Number] [Number] [Number]
[Number] [Number] [Number]
[...]
Run Code Online (Sandbox Code Playgroud)
请注意,有些行有2个数字,有些有3个数字.目前我的输入代码看起来像这些
inputFile >> a >> b >> c;
Run Code Online (Sandbox Code Playgroud)
但是,我想忽略只有2个数字的行,是否有一个简单的解决方法呢?(最好不使用字符串操作和转换)
谢谢
这是一个示例代码:
#include <iostream>
#include <stdexcept>
#include <cstring>
#include <ctime>
#include <sstream>
using std::cout;
using std::endl;
std::size_t const BUF_SIZE(1000);
std::ostream& operator<<(std::ostream& os, std::tm const& rhs)
{
os << asctime(&rhs);
return os;
}
std::istream& operator>>(std::istream& is, std::tm& rhs)
{
while (is.peek() == ' ' || is.peek() == '\t')
{
is.get();
}
std::streampos curPos = is.tellg();
char buf[BUF_SIZE];
is.getline(buf, BUF_SIZE);
char* ptr = strptime(buf, "%D %T", &rhs);
if (ptr == 0)
{
throw std::runtime_error("strptime() failed!");
}
std::size_t processed = ptr - buf;
is.seekg(curPos …Run Code Online (Sandbox Code Playgroud) 就像readome甚至不读.返回0并且不读取任何字符.这有什么不对?
#include <fstream>
#include <iostream>
int main ()
{
std::fstream stream("list.cpp", std::ios::in);
if (stream.good() || !stream.bad() || stream.is_open()) {
std::cout << "Well, stream looks good." << std::endl;
char justOneChar = 'L';
auto ssize = stream.readsome(&justOneChar, 1);
std::cout << ssize << " : " << justOneChar << std::endl;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
输出:
好吧,流看起来不错.0:L
虽然libstdc ++没有,但libc ++确实遵循标准,该标准规定传递ios_base::failbit给basic_istream::exceptions格式化输入没有任何影响.例如这段代码:
istringstream is{"ASD"};
double foo;
is.exceptions(istream::failbit);
try {
is >> foo;
cout << foo << endl;
} catch(ios_base::failure& fail) {
cout << "ouch\n";
}
Run Code Online (Sandbox Code Playgroud)
会导致:
我对LWG2349的阅读是因为它basic_istream不会抛出任何格式化的输入.
例如,LWG2349建议对标准的27.7.2.3 [istream]/1进行更改,该标准引用了一个错误的失效,该错误会使libc ++行为像libstdc ++.更改以粗体显示,并在下面进行说明:
如果在输入期间抛出除抛出的异常
clear()(如果有的话)之外的异常,则ios::badbit在*this错误状态下打开.(抛出的异常如果basic_ios<>::clear()不会被捕获或重新抛出.)(exceptions()&badbit) != 0那么异常被重新抛出.
我明白这basic_istream::clear是对错误的格式化输入的反应,所以我误读了LWG2349还是实际上它会停止basic_istream抛出任何错误?
为什么没有
template <typename T>
T std::from_string(const std::string& s);
Run Code Online (Sandbox Code Playgroud)
在C++标准?(看看有什么std::to_string()功能,我的意思是)
PS - 如果您对未被采纳/考虑的原因有所了解,只需回答/评论并说出它是什么,而不是低估.我实际上并未提出将其纳入标准的提案.
我有以下代码:
#include <iostream>
#include <boost\lexical_cast.hpp>
struct vec2_t
{
float x;
float y;
};
std::istream& operator>>(std::istream& istream, vec2_t& v)
{
istream >> v.x >> v.y;
return istream;
}
int main()
{
auto v = boost::lexical_cast<vec2_t>("1231.2 152.9");
std::cout << v.x << " " << v.y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我从Boost收到以下编译错误:
错误1错误C2338:目标类型既不是std :: istream
able nor std::wistream能够的
这看起来很简单,过去一小时我一直在桌子上打我的头.任何帮助,将不胜感激!
编辑:我正在使用Visual Studio 2013.
许多站点描述了istream :: putback()函数,它允许您将一个字符"放回"到输入流中,以便您可以在后续的读取操作中再次读取它.
但是,阻止我在同一个流上按顺序多次调用putback()会有什么阻碍?当然,你应该在每次操作后检查错误,以确定它是否成功; 然而,我想知道:有没有保证特定类型的流支持一次放回多个字符?
我只是在这里猜测,但我可以想象istringstream能够放回与流中字符串长度一样多的字符; 但我不太确定ifstream的情况是一样的.
这是真的吗?如何才能知道我有多少个字符可以补篮()为istream对象?