简体   繁体   English

Readfile是不是从文本文件中读取空格?

[英]Readfile is not reading the the blank spaces from my text file?

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. 标准输入函数istream::operator>>()在执行输入之前会跳过所有前导空格。 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. 通过设置std::ios_base::noskipws标志,流将不会丢弃前导空白,并且ch将获得每个连续字符的值。 Note that this succeeds only with the overload that takes a char ( ch will be given the value of the space). 请注意,这仅在采用char的重载才能成功( ch将获得空格值)。 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. get()是一个UnformattedInputFunction函数,因此不会事先解析输入。

     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::istreambuf_iterator<>也不解析输入:

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

You are performing formatted input, use unformatted input 您正在执行格式化输入,请使用未格式化输入

std::fstream::traits_type::int_type ch;
while((ch = fin.get()) != std::fstream::traits_type::eof()) {
    // ...
}

By default, operator>> on streams skips any leading whitespace before parsing the value. 默认情况下,流中的operator>>在解析值之前会跳过所有前导空格。 This is, for example, what allows you to read the input 30 60 95 with int i,j,k; fin >> i >> j >> k; 例如,这是使您可以读取带有int i,j,k; fin >> i >> j >> k;的输入30 60 95 int i,j,k; fin >> i >> j >> k; int i,j,k; fin >> i >> j >> k; (otherwise reading j would fail because after the 30 , there follows a space, not an integer). (否则,读取j将会失败,因为在30后面跟随一个空格,而不是整数)。

You now have two options if you want to read the spaces as well: 现在,如果您还想阅读空格,则有两个选择:

  1. (preferred): Use the member function get() for unformatted reading of a character. (首选):使用成员函数get()来无格式读取字符。
  2. Instruct the stream not to eat whitespace before reading: fin >> std::noskipws >> ch . 指示流在阅读前不要吃空格: fin >> std::noskipws >> ch

Read more about the different iostream methods. 阅读有关不同iostream方法的更多信息。 In particular, you are using istream's operator>> . 特别是,您正在使用istream的operator >> Take careful note of how it is designed to work; 仔细记录其设计工作方式; it uses whitespace as a delimiter and does not store the whitespace. 它使用空格作为定界符,并且不存储空格。

If you want to read every char from your stream (eg a file stream), you should not be using >> , but rather consider using istream::get() . 如果要读取流(例如文件流)中的每个char ,则不应使用>> ,而应考虑使用istream :: get()

// stream is an istream, such as cin or ifstream
char ch;
while (ch = stream.get()) { }

This worked for me. 这对我有用。 I set it in a function so you can copy paste. 我将其设置为一个函数,以便您可以复制粘贴。 It detects the space, and also the change in line. 它检测空间,以及行中的变化。 I tried it with ASCII art and it worked fine. 我用ASCII艺术进行了尝试,效果很好。

void print2()
    {
    char ch;
    std::fstream myStream("GameOver.txt", std::fstream::in);
    while (myStream.get(ch))
        {
            std::cout << ch;        
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM