C++ 读取输入文件时不打印空格

C++ 读取输入文件时不打印空格,c++,C++,我试图读取输入文件的每个“char”并写入输出文件,直到找到“?”作为文件的结尾。除了单词之间的空格外,每个字符都写入输出文件。我不知道这个代码出了什么问题 #include <iostream> #include <fstream> using namespace std; int main() { ifstream infile("in.txt"); ofstream outfile("out.txt"); char ch; infile &g

我试图读取输入文件的每个“char”并写入输出文件,直到找到“?”作为文件的结尾。除了单词之间的空格外,每个字符都写入输出文件。我不知道这个代码出了什么问题

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
   ifstream infile("in.txt");
   ofstream outfile("out.txt");
   char ch;
   infile >> ch;
   while(ch != '?')
   {
     outfile<<ch;
     infile >> ch;
   }
}
istream运算符>>忽略空白。试试这个:

std::string s;
std::getline(infile,s,'?');
outfile << s;
istream运算符>>忽略空白。试试这个:

std::string s;
std::getline(infile,s,'?');
outfile << s;
尝试在读取时使用

infile >> noskipws >> ch;
noskipws告诉输入流不要跳过默认情况下的空白。

尝试使用on read

infile >> noskipws >> ch;

noskipws告诉输入流不要跳过默认情况下的空白。

输入流的>>运算符通常与解释关联。例如,当读取字符串时,它跳过空格。读取字符时可能会出现这种情况。 您应该使用read方法,例如:

infile.read(&ch, 1)
请参阅以供参考


编辑我忘记了get方法。这将得到一个字符,转换为int。read方法更适合于在一次调用中读取数据块。

输入流的>>运算符通常与解释关联。例如,当读取字符串时,它跳过空格。读取字符时可能会出现这种情况。 您应该使用read方法,例如:

infile.read(&ch, 1)
请参阅以供参考


编辑我忘记了get方法。这将得到一个字符,转换为int。read方法更适合于在一次调用中读取一块数据。

@Andrew White已经指出了如何解决您看到的问题。我会提出我的想法,通常是我自己的想法,可能是关于如何完成剩余工作的过度设计的想法:

#pragma once
#if !defined(SENTINEL_ITERATOR_H_)
#define  SENTINEL_ITERATOR_H_
#include <istream>
#include <iterator>

template <class T,
          class charT=char,
          class traits=std::char_traits<charT>,
          class distance = ptrdiff_t>

class sentinel_iterator :
    public std::iterator<std::input_iterator_tag,distance,void,void,void>
{
    std::basic_istream<charT,traits> *is;
    T value;
public:
    typedef charT char_type;
    typedef traits traits_type;
    typedef std::basic_istream<charT,traits> istream_type;

    sentinel_iterator(istream_type& s)
        : is(&s)
    { s >> value; }

    sentinel_iterator(T const &s) : is(0), value(s) { }

    const T &operator*() const { return value;  }
    const T *operator->() const { return &value; }

    sentinel_iterator &operator++() {
        (*is)>>value;
        return *this;
    }

    sentinel_iterator &operator++(int) {
        sentinel_iterator tmp = *this;
        (*is)>>value;
        return (tmp);
    }

    bool operator==(sentinel_iterator<T,charT,traits,distance> const &x) {
        return value == x.value;
    }

    bool operator!=(sentinel_iterator<T,charT,traits,distance> const &x) {
        return !(value == x.value);
    }
};

#endif 
然后代码变成如下所示:

#include <algorithm>
#include <fstream>
#include "sentinel_iterator.h"

int main() { 
    ifstream infile("in.txt");
    ofstream outfile("out.txt");

    infile >> noskipws;

    std::copy(sentinel_iterator<char>(infile),
              sentinel_iterator<char>('?'),
              std::ostream_iterator<char>(outfile));
    return 0;
}

@安德鲁·怀特已经指出了如何解决你所看到的问题。我会提出我的想法,通常是我自己的想法,可能是关于如何完成剩余工作的过度设计的想法:

#pragma once
#if !defined(SENTINEL_ITERATOR_H_)
#define  SENTINEL_ITERATOR_H_
#include <istream>
#include <iterator>

template <class T,
          class charT=char,
          class traits=std::char_traits<charT>,
          class distance = ptrdiff_t>

class sentinel_iterator :
    public std::iterator<std::input_iterator_tag,distance,void,void,void>
{
    std::basic_istream<charT,traits> *is;
    T value;
public:
    typedef charT char_type;
    typedef traits traits_type;
    typedef std::basic_istream<charT,traits> istream_type;

    sentinel_iterator(istream_type& s)
        : is(&s)
    { s >> value; }

    sentinel_iterator(T const &s) : is(0), value(s) { }

    const T &operator*() const { return value;  }
    const T *operator->() const { return &value; }

    sentinel_iterator &operator++() {
        (*is)>>value;
        return *this;
    }

    sentinel_iterator &operator++(int) {
        sentinel_iterator tmp = *this;
        (*is)>>value;
        return (tmp);
    }

    bool operator==(sentinel_iterator<T,charT,traits,distance> const &x) {
        return value == x.value;
    }

    bool operator!=(sentinel_iterator<T,charT,traits,distance> const &x) {
        return !(value == x.value);
    }
};

#endif 
然后代码变成如下所示:

#include <algorithm>
#include <fstream>
#include "sentinel_iterator.h"

int main() { 
    ifstream infile("in.txt");
    ofstream outfile("out.txt");

    infile >> noskipws;

    std::copy(sentinel_iterator<char>(infile),
              sentinel_iterator<char>('?'),
              std::ostream_iterator<char>(outfile));
    return 0;
}

或者告诉它不要使用noskipws查看答案。不过,getline可能会更快。@Andrew:哦,是的,我忘了。或者用noskipws告诉它不要看答案。不过,getline可能会更快。@Andrew:哦,是的,我忘了。谢谢@Andrew,它能工作。我有一个问题=>如果有空格,阅读文件的根本错误是跳过空格???@Alok:别忘了接受答案,点击绿色复选框并投上一票:@Alok:无论如何,通过查看你的个人资料,我打赌你知道你在做什么;祝你的项目好运Hanks@Andrew成功了。我有一个问题=>如果有空格,阅读文件的根本错误是跳过空格???@Alok:别忘了接受答案,点击绿色复选框并投上一票:@Alok:无论如何,通过查看你的个人资料,我打赌你知道你在做什么;祝你的项目好运