读取输入文件时空格不打印 [英] spaces are not printing when reads the input file

查看:377
本文介绍了读取输入文件时空格不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read each 'char' of the input file and write in the output file until finds the '?' as the end of file . Every char is written in output file except the spaces between words. I dont know what's wrong in this code??

#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;
   }
}


推荐答案

在阅读时使用 noskipws ...

Try using noskipws on read...

infile >> noskipws >> ch;

noskipws告诉输入流不跳过默认情况下执行的空格。

The noskipws tells the input stream to not skip whitespace which it does by default.

这篇关于读取输入文件时空格不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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