Readfile doesn't read spaces from my text file?

I read a text file and found that it would not print spaces between words. I want to read each character character at a time, and then print the character in the output window. Reading will read the file, but does not display spaces, and I could not figure out why spaces are skipped.

Question: Why is my reading not reading empty characters in the 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... 
+1
source share
5 answers

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

  • std::noskipws

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

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

    get() is a function of UnformattedInputFunction and, therefore, will not parse input in advance.

     while (fin.get(ch)) { // ... } 
  • std::istreambuf_iterator<>

    You can also use iterators to work directly with the buffer. std::istreambuf_iterator<> also does not parse input:

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

You perform formatted input, use unformatted input

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

By default, operator>> in threads skips all leading spaces before parsing the value. This, for example, allows you to read input 30 60 95 using int i,j,k; fin >> i >> j >> k; int i,j,k; fin >> i >> j >> k; (otherwise, reading j will fail because 30 followed by a space, not an integer).

You have two options if you want to read spaces as well:

  • (preferred): Use the get() member function to read the character unformatted.
  • To instruct the stream is not a space before reading: fin >> std::noskipws >> ch .
0
source

Learn more about the various iostream methods. In particular, you use the istream operator "> . Take care of how it is designed to work; it uses spaces as a delimiter and does not save spaces.

If you want to read every char from your stream (e.g. a file stream), you should not use >> , but rather consider using IStream :: get () .

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

It worked for me. I set it to a function so that you can copy the paste. It defines space as well as row change. I tried this with ASCII art and it worked great.

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

All Articles