Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 输入迭代器跳过空白,是否有任何方法防止此跳过_C++_Istream - Fatal编程技术网

C++ 输入迭代器跳过空白,是否有任何方法防止此跳过

C++ 输入迭代器跳过空白,是否有任何方法防止此跳过,c++,istream,C++,Istream,我从一个文件读入一个字符串,直到到达一个定界字符,即美元符号。但是输入迭代器跳过空白,因此创建的字符串没有空格。这不是我想要的。有没有办法阻止跳绳行为?如果是这样,怎么办 这是我的测试代码 #include <iostream> #include <fstream> #include <iterator> #include <string> // istream iterator is skipping whitespace. How do I

我从一个文件读入一个字符串,直到到达一个定界字符,即美元符号。但是输入迭代器跳过空白,因此创建的字符串没有空格。这不是我想要的。有没有办法阻止跳绳行为?如果是这样,怎么办

这是我的测试代码

#include <iostream>
#include <fstream>
#include <iterator>
#include <string>

// istream iterator is skipping whitespace.  How do I get all chars?
void readTo(std::istream_iterator<char> iit, 
            std::string& replaced)
{
   while(iit != std::istream_iterator<char>()) {
     char ch = *iit++;
     if(ch != '$')
      replaced.push_back(ch);
     else
        break;
   }
}

int main() {
   std::ifstream strm("test.txt");
   std::string s;
   if(strm.good()) {
       readTo(strm, s);
       std::cout << s << std::endl;
   }

    return 0;
}
#包括
#包括
#包括
#包括
//istream迭代器正在跳过空白。我怎样才能得到所有的字符?
void readTo(标准::istream_迭代器iit,
std::字符串(已替换)
{
while(iit!=std::istream_迭代器()){
char ch=*iit++;
如果(ch!=“$”)
更换。向后推(ch);
其他的
打破
}
}
int main(){
std::ifstream strm(“test.txt”);
std::字符串s;
if(strm.good()){
readTo(strm,s);

std::cout因为默认情况下流被配置为跳过空白,因此,使用

noskipws(strm);

标准:

基本ios构造函数
explicit basic_ios(basic_streambuf*sb);
效果:构造类basic_ios的对象,通过调用
init(sb)将初始值分配给其成员对象。

basic_ios();
效果:构造类
basic_ios
(27.5.2.7)的对象,使其成员对象未初始化。 该对象应通过调用其
init
成员函数进行初始化。如果该对象在被销毁之前已被销毁 初始化该行为未定义

[……]

void init(basic_streambuf*sb);
后置条件:该功能的后置条件如表118所示

+----------+-------------+
| ...      | ...         |
| flags()  | skipws|dec  | 
| ...      | ...         |
+----------+-------------+
  (Table 118)