简体   繁体   中英

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. 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}, 

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. 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; (otherwise reading j would fail because after the 30 , there follows a space, not an integer).

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.
  2. Instruct the stream not to eat whitespace before reading: fin >> std::noskipws >> ch .

Read more about the different iostream methods. In particular, you are using istream's 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() .

// 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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