Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) StdIfstreamExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppStdIfstreamExample1/CppStdIfstreamExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

#
#
# Type of compile
#
#

CONFIG(release, debug|release) {
  DEFINES += NDEBUG NTRACE_BILDERBIKKEL
}

QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++

unix {
  QMAKE_CXXFLAGS += -Werror
}

#
#
# Boost
#
#

win32 {
  INCLUDEPATH += \
    ../../Libraries/boost_1_54_0
}

 

 

 

 

 

./CppStdIfstreamExample1/main.cpp

 

#include <cassert>
#include <fstream>
#include <iostream>
#include <vector>

///DangerDoNotDoThis demonstrates that if a non-existing file is read,
///the program will do this without warning and infinitely
void DangerDoNotDoThis()
{
  //Assure that the file does not exist
  const std::string filename { "tmp.txt" };
  std::remove(filename.c_str());

  //'Open' the nonexisting file
  std::ifstream f(filename.c_str());

  //This test will prevent all nasty things from happening!
  //assert(f.is_open());

  //Read until the end of the (non-existing) file. For a non-existing file
  //this will read infinitely
  for (int i=0; !f.eof(); ++i)
  {
    std::string s;
    std::getline(f,s);
    //Display a dot and s (which will be empty) without a newline
    //This will cause the whole screen to fill up with dots
    std::cout << "." << s << std::flush;
  }

}

int main()
{
  DangerDoNotDoThis();
}

/* Screen output (copied after breaking the program):

................................................................................
................................................................................
................................................Press <RETURN> to close this win
dow...

*/

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml