Readfile不是从我的文本文件读取空白空间? [英] Readfile is not reading the the blank spaces from my text file?

查看:174
本文介绍了Readfile不是从我的文本文件读取空白空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一个文本文件,发现它不会打印字之间的空白字符。我想一次读取每个字符一个字符,然后将字符打印到输出窗口。读取将读取文件,但不显示空格,我无法找到为什么空白被跳过。



问题:为什么我的读取不会读取测试文件中的空白字符?



当我找到一个空白字符时, p>

代码:

  #includestdafx.h
#includeiostream
#include< iostream>
#include< fstream>

void readTestFile()
{
char ch;
std :: fstream fin(C:/Users/itpr13266/Desktop/myTest.txt,std :: fstream :: in);
while(fin>> ch){
std :: cout< Letter:<< ch < std :: endl;
if(ch =='')< - 应该捕获一个空格
{
std :: cout< 空白<< std :: endl;
}
else< - 只写写信
{
std :: cout< ch < std :: endl;
}
}
}

int _tmain(int argc,_TCHAR * argv [])
{
readTestFile();

getchar();
return 0;
}

测试文件:

 这是为fprintf ...测试... 
这是测试fputs ...

输出

  Letter:T 
T

h
... etc ...


解决方案>

标准输入函数 istream :: operator>>()在执行输入之前跳过所有前导空格。如果您需要获取空格,可以使用以下几个选项:




  • std :: noskipws



    通过设置 std :: ios_base :: noskipws 标志,流不会丢弃前导空格, ch 将给出每个连续字符的值。请注意,只有使用 char ch 会给出空格的值)的重载才能成功。对于任何其他数据类型,这将不起作用:

      while(fin>> std: :noskipws>> ch)
    {
    // ...
    }


  • std :: istream :: get()



    get()是一个UnformattedInputFunction函数,因此不会预先解析输入。

      while(fin.get(ch))
    {
    // ...
    }


  • std :: istreambuf_iterator<>



    您还可以使用迭代器直接与缓冲区一起工作。 std :: istreambuf_iterator<> 也不解析输入:

      std :: copy(std :: istreambuf_iterator< char> {fin},
    std :: istreambuf_iterator< char> {},
    std :: ostreambuf_iterator< char> {std :: cout},



I am reading in a text file and have found that it will not print the blank characters between the words. I want to read each character a character at a time and then print the character to the output window. The read will read the file but does not show the blank spaces and I have not been able to find out why the blank spaces are being skipped.

Question: Why is my read not reading the blank characters in my test file?

When i find a blank character I want to print the word Blank Space.

Code:

#include "stdafx.h"
#include "iostream"
#include<iostream>
#include<fstream>

void readTestFile()
{
    char ch;
    std::fstream fin("C:/Users/itpr13266/Desktop/myTest.txt", std::fstream::in);
    while (fin >> ch) {
        std::cout << "Letter: " << ch << std::endl;
          if (ch == ' ')  <-- should catch a blank spaces
          {
              std::cout << "Blank Space" << std::endl;
          }
          else  <-- Just write the letter
          {
              std::cout << ch << std::endl; 
          }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
   readTestFile();

   getchar();
   return 0;
}

Test File:

  This is testing for fprintf...
  This is testing for fputs...

Output

 Letter: T
 T
 Letter: h
 h
 ...etc...

解决方案

The standard input function istream::operator>>() skips all leading whitespace before performing input. If you need to obtain spaces, there are a couple options you can use:

  • std::noskipws

    By setting the std::ios_base::noskipws flag, the stream will not discard leading whitespace and ch will be given the value of each consecutive character. Note that this succeeds only with the overload that takes a char (ch will be given the value of the space). For any other data type this will not work:

    while (fin >> std::noskipws >> ch)
    {
        // ...
    }
    

  • std::istream::get()

    get() is an UnformattedInputFunction function, and thus will not parse the input beforehand.

    while (fin.get(ch))
    {
        // ...
    }
    

  • std::istreambuf_iterator<>

    You can also use iterators to work directly with the buffer. std::istreambuf_iterator<> also doesn't parse the input:

    std::copy(std::istreambuf_iterator<char>{fin},
              std::istreambuf_iterator<char>{},
              std::ostreambuf_iterator<char>{std::cout},
    

这篇关于Readfile不是从我的文本文件读取空白空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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