Reading from Ifstream Won't Read Whitespace

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

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

File reading with noskipws?

Your code is correct in that it reads whitespace and newlines but the error checking for the input is incorrectly positioned and this can be shortened with the use of istream::get().

char word_from_file;
while (input_file.get(word_from_file)) {
if (*recieved_choice == 1) {
cout << *recieved_key;
encrypt (recieved_file_name, &word_from_file, recieved_key);
}
}

istream::get() reads an unformatted character from the stream, so it will automatically read whitespace and newlines.

There's also no need to check if the file opened or to manually close it. The file will close automatically at the end of the scope in which its created, and any attempted input operation is a no-op if the file didn't open.

C++ ifstream not reading \n?

The >> operator does a "formatted input operation" which means (among other things) it skips whitespace.

To read raw characters one by one without skipping whitespace you need to use an "unformatted input operation" such as istream::get(). Assuming value is of type char, you can read each char with instream.get(value)

When you reach EOF the read will fail, so you can read every character in a loop such as:

while (instream.get(value))
// process value

However, to read line-by-line you could read into a std::string and use std::getline

std::string line;
while (getline(instream, line))
// ...

This is an unformatted input operation which reads everything up to a \n into the string, then discards the \n character (so you'd need to manually append a \n after each erad line to reconstruct the original input)

ifstream ignoring spaces and new lines - why?

The operator >> eats any whitespaces (newline, tab, space). If you need to count the number of lines, you can use the getline function.

#include <cassert>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
unsigned lines = 0;
string line_content;

ifstream r_tftpd_hpa ("tftpd-hpa");
assert(r_tftpd_hpa);

while ( getline(r_tftpd_hpa, line_content) ) {
lines++;

if ( line_content[0] == ' ' ) { // my failed attempt at catching spaces
cout << endl << "Found empty line: " << lines << endl;
}

cout << "Line: " << lines << " content: " << line_content << endl;
}

return 0;
}

gives me:

Line: 1 content: # /etc/default/tftpd-hpa
Line: 2 content:
Line: 3 content: TFTP_USERNAME="tftp"
Line: 4 content: TFTP_DIRECTORY="/var/lib/tftpboot"
Line: 5 content: TFTP_ADDRESS="0.0.0.0:69"
Line: 6 content: TFTP_OPTIONS="--secure"


Related Topics



Leave a reply



Submit