Reading from ifstream won't read whitespace

ifstream getline
c++ program to read and write a text file
getline c++
ifstream read one character
c++ open
c++ fstream read
c++ read string with spaces from file

I'm implementing a custom lexer in C++ and when attempting to read in whitespace, the ifstream won't read it out. I'm reading character by character using >>, and all the whitespace is gone. Is there any way to make the ifstream keep all the whitespace and read it out to me? I know that when reading whole strings, the read will stop at whitespace, but I was hoping that by reading character by character, I would avoid this behaviour.

Attempted: .get(), recommended by many answers, but it has the same effect as std::noskipws, that is, I get all the spaces now, but not the new-line character that I need to lex some constructs.

Here's the offending code (extended comments truncated)

while(input >> current) {
    always_next_struct val = always_next_struct(next);
    if (current == L' ' || current == L'\n' || current == L'\t' || current == L'\r') {
    if (current == L'/') {
        input >> current;
        if (current == L'/') {
            // explicitly empty while loop
            while(input.get(current) && current != L'\n');

I'm breaking on the while line and looking at every value of current as it comes in, and \r or \n are definitely not among them- the input just skips to the next line in the input file.

There is a manipulator to disable the whitespace skipping behavior:

stream >> std::noskipws;

How can I open a stream in binary mode?, C++ FAQ, #include <string> #include <iostream> #include <fstream> void Note: input >> c discards leading whitespace, so you won't normally use that when reading  Use ifstream to read data from a file: std::ifstream input( "filename.ext" ); If you really need to read line by line, then do this: for( std::string line; getline( input, line ); ) { for each line in input } But you probably just need to extract coordinate pairs: int x, y; input >> x >> y; Update:

The operator>> eats whitespace (space, tab, newline). Use yourstream.get() to read each character.


Beware: Platforms (Windows, Un*x, Mac) differ in coding of newline. It can be '\n', '\r' or both. It also depends on how you open the file stream (text or binary).

Edit (analyzing code):


  while(input.get(current) && current != L'\n');

there will be an \n in current, if not end of file is reached. After that you continue with the outmost while loop. There the first character on the next line is read into current. Is that not what you wanted?

I tried to reproduce your problem (using char and cin instead of wchar_t and wifstream):

//: get.cpp : compile, then run: get < get.cpp

#include <iostream>

int main()
  char c;

  while (std::cin.get(c))
    if (c == '/') 
      char last = c; 
      if (std::cin.get(c) && c == '/')
        // std::cout << "Read to EOL\n";
        while(std::cin.get(c) && c != '\n'); // this comment will be skipped
        // std::cout << "go to next line\n";
     else { std::cin.putback(c); c = last; }
    std::cout << c;
  return 0;

This program, applied to itself, eliminates all C++ line comments in its output. The inner while loop doesn't eat up all text to the end of file. Please note the putback(c) statement. Without that the newline would not appear.

If it doesn't work the same for wifstream, it would be very strange except for one reason: when the opened text file is not saved as 16bit char and the \n char ends up in the wrong byte...

[PDF] Using C++ File Streams, File streams come in two flavors also: the class ifstream (input file stream) nothing about except try to muddle through (maybe it won't happen a second time​)  This. ifstream input_file("blah.txt", ios::in); should open the file:. Additionally, when the second constructor version is used, the stream is associated with a physical file as if a call to the member function open with the same parameters was made.

You could open the stream in binary mode:

std::wifstream stream(filename, std::ios::binary);

You'll lose any formatting operations provided my the stream if you do this.

The other option is to read the entire stream into a string and then process the string:

std::wostringstream ss;
ss << filestream.rdbuf();

OF course, getting the string from the ostringstream rquires an additional copy of the string, so you could consider changing this at some point to use a custom stream if you feel adventurous. EDIT: someone else mention istreambuf_iterator, which is probably a better way of doing it than reading the whole stream into a string.

C++ Files, Example. #include <iostream> #include <fstream> using namespace std; int main​()  I'm trying to read a series of floats from a .out file using ifstream, but if I output them afterwards, they are not correct. This is my input code: float x, y, z; ifstream table;"

Wrap the stream (or its buffer, specifically) in a std::streambuf_iterator? That should ignore all formatting, and also give you a nice iterator interface.

Alternatively, a much more efficient, and fool-proof, approach might to just use the Win32 API (or Boost) to memory-map the file. Then you can traverse it using plain pointers, and you're guaranteed that nothing will be skipped or converted by the runtime.

End of File Function --C++, Always test for the end-of-file condition before processing data read from an input file stream. a. use a priming ifstream fin; // declare stream variable name. ifstream has ios_base::in by default. You don't need to specify it. operator>> can be invoked directly as an operator: in >> a. Reading strings is the same: in >> s, but the caveat is that it is whitespace-delimited, so it will read "just" by itself, without "dummy". If you want to read complete lines, use std::getline(in, s).

The stream extractors behave the same and skip whitespace.

If you want to read every byte, you can use the unformatted input functions, like stream.get(c).

Intro to File Input/Output in C++, ifstream inFile; // object for reading from a file ofstream outFile; // object for writing to read that line (since there is no integer to read) and it won't advance to the  C++ can I reuse fstream to open and write multiple files? why is it not possible to use one ifstream variable for opening one file, reading it, then closing it and, after that, opening another file, reading and closing, etc? How would that look in code (let's just say each file has an integer inside):

[PDF] C++ File Reading Basic workflow: (1) open file, (1a) check to make , ifstream infile;. // Input file stream"nums.txt");. // Step 1: Open file if (!​infile.is_open()) {. // Step 1a: check for opening error cout << "Could not open file​  C++ (Cpp) ifstream::read - 30 examples found. These are the top rated real world C++ (Cpp) examples of ifstream::read extracted from open source projects. You can rate examples to help us improve the quality of examples.

Why won't ifstream work? - C++ Forum, Hi, I'm trying to write a script to read from a file and I never had problems before reading/writing to a file, but with the last few times ifstream just  Well, since the stuff always comes in pairs, just read the file two lines at a time. Search the first line to see if you can find() the thing the user typed. If it is found, print the second line. Otherwise, read the next two lines and repeat. Hope this helps.

Program won't open file? - C++ Forum, This program reads data from a file. #include <iostream> #include <fstream> using namespace std; int main() { ifstream inputFile; int number;  Note that even though ifstream is an input stream, its internal filebuf object may be set to also support output operations. If the mode has app set, the opening operation fails. It also fails if trunc is set but out is not.