how to ignore lines when writing to a file - c++

how can I write to a file at the nth line (for example the 5th line) in c++?
here's my attempt:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream stream1("1.txt");
string line ;
ofstream stream2("2.txt");
int lineNumber = 0;
while(getline( stream1, line ) )
{
if (lineNumber == 5)
{
stream2 << "Input" << endl;
lineNumber = 0;
}
lineNumber++;
}
stream1.close();
stream2.close(); return 0;
}
in "1.txt", I have the word "Student" at the 4th line, now I want to ignore the above 4 lines and input the word "Input" at the 5th line (below the word "Student"). When I run the above code, the output file is blank. Any suggestion how to fix this? Thanks.

If I understand it right, all you want is a replica of 1.txt in 2.txt with just the specific line number replaced with your personal content.
In your case it seems, the word is "Input".
Well here is a code that I modified from your original one -
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream stream1("1.txt");
string line ;
ofstream stream2("2.txt");
int lineNumber = 0;
int line_to_replace = 4; //This will replace 5th line
while(getline( stream1, line ) )
{
if (lineNumber == line_to_replace)
{
stream2 << "Input" << endl;
}
else
stream2 << line << endl;
lineNumber++;
}
stream1.close();
stream2.close();
return 0;
}
Input File (1.txt) -
sdlfknas
sdfas
sdf
g
thtr
34t4
bfgndty
45y564
grtg
Output File (2.txt) -
sdlfknas
sdfas
sdf
g
Input
34t4
bfgndty
45y564
grtg
p.s. To learn and understand programming better, I would recommend not to use:
using namespace std;

When you're reading the 5th line, lineNumber equals 4 b/c you start your counting at 0.
Change if(lineNumber == 5)
to if(lineNumber == 4) You also have an issue where you're setting lineNumber = 0 then immediately incrementing to 1, so you're only going to count 4 lines before outputting again.

I would create a function like this...
bool isBlank(string line) {
if (!line.empty()) {
for (auto x: line) {
if (isalnum(x)) {
return false;
}
}
}
return true;
}
It returns true if a string is empty or has no alphanumeric characters.
You can call this function right after the getline statement.
The isalnum function is specified in <cctype>

After working with your code I managed to get the output that you desired. Here is the updated version of your code.
#include <iostream>
#include <fstream>
int main() {
std::ifstream stream1( "1.txt" );
std::string line;
std::ofstream stream2( "2.txt" );
int lineNumber = 1;
while ( getLine( stream1, line ) ) {
if ( lineNumber == 5 ) {
stream2 << "Input" << std::endl;
} else {
stream2 << std::endl;
lineNumber++;
}
}
stream1.close();
stream2.close();
return 0;
}
The one thing you have to make sure is that in your 1.txt that has the word student on the 4th line is that you must have at least 2 empty lines after this text in the file. A simple enter or carriage return will do! If you do not the while( getline() ) will go out of scope and it will not read the next line and the code block will never enter your if() statement when lineNumber == 5 and it will not print the text "Input" to your stream2 file stream object.
If your last line of text in your 1.txt file is the line with the string of text Student what happens here is it will add this line of text to your line string variable then the code will increment your lineNumber to equal 5. The next time you go into the while loop to call getline() it returns false because you are at the EOF since there are no more lines of text from the file to read in and this causes the while loop to break out of execution and it goes out of scope and the if( lineNumber == 5 ) never gets called because it is nested within the while loop's scope.

My first answer addressed the issue with your problem and getting the output to your text file appropriately. However as I mentioned about the while loop for reading in a line of text and using the same counter for both file streams is not very elegant. A more accurate way to do this which will also allow for debugging to be simplified would be to read in your full input file one line at a time and save each line into a string while storing your strings in a vector. This way you can parse each line of text that you need one at a time and you can easily traverse your vector to quickly find your line of text. You should also do checks to make sure your file exists and that it opens correctly.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main() {
std::string strTextFileIn( "1.txt" );
std::ifstream in;
std::string strLine;
std::vector<std::string> vFileContents;
// Open File Stream
in.open( strTextFileIn.c_str(), std::ios_base::in );
// Test To Make Sure It Opened Properly And That It Exists
if ( !in.is_open() ) {
std::cout << "Failed to open file, check to make sure file exists." << std::endl;
return -1;
} else {
while ( !in.eof() ) {
// Get Line Of Text And Save To String
std::getline( in, strLine );
// Push String Into Vector
vFileContents.push_back( strLine );
}
}
// Done With File Close File Stream
if ( in.is_open() ) {
in.close();
}
// Now That We Have Read In The File Contents And Have Saved Each Line Of Text To A String
// And Stored It In Our Container (Vector) We Can Traverse Our Vector To Find Which String
// Matches The Text We Are Looking For Retrive Its Indexed Value And Then Write To Our
// Output File According To Where You Want The Text To Be Written
unsigned index = 0;
const std::string strLookup( "Student" );
for ( unsigned int i = 0; i < vFileContents.size(); i++ ) {
if ( vFileContents[i] == strLookup ) {
// Strings Match We Have Our Indexed Value
index = i;
}
}
// We Want To Write Our Line Of Text 1 Line Past Our Index Value As You Have Stated.
std::string strTextFileOut( "2.txt" );
std::ofstream out;
// Open Our File For Writting
out.open( strTextFileOut.c_str(), std::ios_base::out );
if ( !out.is_open() ) {
std::cout << "Failed To open file.";
vFileContents.clear();
return -1;
} else {
for ( unsigned int i = 1; i <= index; i++ ) {
out << std::endl; // Write Blank Lines
}
// The Text Or String You Want To Write To File
out << "Input" << std::endl;
}
// Done With File Stream
if ( in.is_open() ) {
in.close();
}
// Clear Out Vector
vFileContents.clear();
return 0;
} // main
Now this can be simplified a bit more by creating a class hierarchy for working with various file stream object types so that you don't have to write this code to open, close, check validity, read in full file or by line over and over again everywhere you need it. This makes it modular. However this structure relies on a few other classes such as an ExceptionHandler class and a Logger class. Below is a small multi file application.
stdafx.h NOTE: Not all of these includes and defines will be used here, but this is coming from a larger project of mine and I'm stripping out only the classes that are needed here, but leaving my standard header as is. The only contents that I stripped out of this "stdafx.h" is anything that has to deal with OpenGL, OpenAL, Ogg - Vorbis, GLM Libraries & APIs
#ifndef STDAFX_H
#define STDAFX_H
#define VC_EXTRALEAN // Exclude Rarely Used Stuff Windows Headers - Windows Only
// Instead of Creating Another File That VS Makes For You "targetver.h"
// I Will Just Append Its Contents Here
#include <SDKDDKVer.h> // Windows Only
#include <Windows.h> // Windows Only
#include <process.h>
#include <tchar.h>
#include <conio.h>
#include <memory>
#include <string>
#include <numeric>
#include <vector>
#include <array>
#include <unordered_map>
#include <queue>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include "ExceptionHandler.h"
namespace pro {
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
};
extern const unsigned INVALID_UNSIGNED;
extern const unsigned INVALID_UNSIGNED_SHORT;
} // namespace pro
#endif // STDAFX_H
stdafx.cpp
#include "stdafx.h"
namespace pro {
const unsigned INVALID_UNSIGNED = static_cast<const unsigned>( -1 );
const unsigned INVALID_UNSIGNED_SHORT = static_cast<const unsigned short>( -1 );
} // namespace pro
ExceptionHandler.h
#ifndef EXCEPTION_HANDLER_H
#define EXCEPTION_HANDLER_H
namespace pro {
class ExceptionHandler sealed {
private:
std::string m_strMessage;
public:
explicit ExceptionHandler( const std::string& strMessage, bool bSaveInLog = true );
explicit ExceptionHandler( const std::ostringstream& strStreamMessage, bool bSavedInLog = true );
// ~ExceptionHandler(); // Default Okay
// ExeptionHandler( const ExceptionHandler& c ); // Default Copy Constructor Okay & Is Needed
const std::string& getMessage() const;
private:
ExceptionHandler& operator=( const ExceptionHandler& c ); // Not Implemented
}; // ExceptionHandler
} // namespace pro
#endif // EXCEPTION_HANDLER_H
ExceptionHandler.cpp
#include "stdafx.h"
#include "ExceptionHandler.h"
#include "Logger.h"
namespace pro {
ExceptionHandler::ExceptionHandler( const std::string& strMessage, bool bSaveInLog ) :
m_strMessage( strMessage ) {
if ( bSavedInLog ) {
Logger::log( m_strMessage, Logger::TYPE_ERROR );
}
}
ExceptionHandler::ExceptionHandler( const std::ostringstream& strStreamMessage, bool bSaveInLog ) :
m_strMessage( strStreamMessage.str() ) {
if ( bSaveInLog ) {
Logger::log( m_strMessage, Logger::TYPE_ERROR );
}
}
const std::string& ExceptionHandler::getMessage() const {
return m_strMessage;
}
} // namespace pro
BlockThread.h -- Needed For Logger
#ifndef BLOCK_THREAD_H
#define BLOCK_THREAD_H
namespace pro {
class BlockThread sealed {
private:
CRITICAL_SECTION* m_pCriticalSection;
public:
explicit BlockThread( CRITICAL_SECTION& criticalSection );
~BlockThread();
private:
BlockThread( const BlockThread& c ); // Not Implemented
BlockThread& operator=( const BlockThread& c ); // Not Implemented
}; // BlockThread
} // namespace pro
#endif // BLOCK_THREAD_H
BlockThread.cpp
#include "stdafx.h"
#include "BlockThread.h"
namespace pro {
BlockThread::BlockThread( CRTICAL_SECTION& criticalSection ) {
m_pCriticalSection = &criticalSection;
EnterCriticalSection( m_pCriticalSection );
}
BlockThread::~BlockThread() {
LeaveCriticalSection( m_pCriticalSection );
}
} // namespace pro
Logger is a Singleton since you will only want one instance of it while your application is running.
Singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H
namespace pro {
class Singleton {
public:
enum SingletonType {
TYPE_LOGGER = 0, // Must Be First!
// TYPE_SETTINGS,
// TYPE_ENGINE,
};
private:
SingletonType m_eType;
public:
virtual ~Singleton();
protected:
explicit Singleton( SingletonType eType );
void logMemoryAllocation( bool isAllocated ) const;
private:
Singleton( const Singleton& c ); // Not Implemented
Singleton& operator=( const Singleton& c ); // Not Implemented
}; // Singleton
} // namespace pro
#endif // SINGLETON_H
Singleton.cpp
#include "stdafx.h"
#include "Logger.h"
#include "Singleton.h"
//#include "Settings.h"
namespace pro {
struct SingletonInfo {
const std::string strSingletonName;
bool isConstructed;
SingletonInfo( const std::string& strSingletonNameIn ) :
strSingletonName( strSingletonNameIn ),
isConstructed( false ) {}
};
// Order Must Match Types Defined In Singleton::SingletonType enum
static std::array<SingletonInfo, 1> s_aSingletons = { SingletonInfo( "Logger" ) }; /*,
SingletonInfo( "Settings" ) };*/ // Make Sure The Number Of Types Matches The Number In This Array
Singleton::Singleton( SingletonType eType ) :
m_eType( eType ) {
bool bSaveInLog = s_aSingletons.at( TYPE_LOGGER ).isConstructed;
try {
if ( !s_aSingletons.at( eType ).isConstructed ) {
// Test Initialization Order
for ( int i = 0; i < eType; ++i ) {
if ( !s_aSingletons.at( i ).isConstructed ) {
throw ExceptionHandler( s_aSingletons.at( i ).strSingletonName + " must be constructed before constructing " + s_aSingletons.at( eType ).strSingletonName, bSaveInLog );
}
}
s_aSingletons.at( eType ).isConstructed = true;
/*if ( s_aSingletons.at( TYPE_ENGINE ).isConstructed &&
Setttings::get()->isDebugLogginEnabled( Settings::DEBUG_MEMORY ) ) {
logMemoryAllocation( true );
}*/
} else {
throw ExceptionHandler( s_aSingletons.at( eType ).strSingletonName + " can only be constructed once.", bSaveInLog );
}
} catch ( std::exception& ) {
// eType Is Out Of Range
std::ostringstream strStream;
strStream << __FUNCTION__ << " Invalid Singleton Type Specified: " << eType;
throw ExceptionHandler( strStream, bSaveInLog );
}
}
Singleton::~Singleton() {
/*if ( s_aSingletons.at( TYPE_ENGINE ).isConstructed &&
Settings::get()->isDebugLoggingEnabled( Settings::DEBUG_MEMORY ) ) {
logMemoryAllocation( false );
}*/
s_aSingletons.at( m_eType ).isConstructed = false;
}
void Singleton::logMemoryAllocation( bool isAllocated ) const {
if ( isAllocated ) {
Logger::log( "Created " + s_aSingletons.at( m_eType ).strSingletonName );
} else {
Logger::log( "Destroyed " + s_aSingletons.at( m_eType ).strSingletonName );
}
}
} // namespace pro
Logger.h
#ifndef LOGGER_H
#define LOGGER_H
#include "Singleton.h"
namespace pro {
class Logger sealed : public Singleton {
public:
// Number Of Items In Enum Type Must Match The Number
// Of Items And Order Of Items Stored In s_aLogTypes
enum LoggerType {
TYPE_INFO = 0,
TYPE_WARNING,
TYPE_ERROR,
TYPE_CONSOLE,
}; // LoggerType
private:
std::string m_strLogFilename;
unsigned m_uMaxCharacterLength;
std::array<std::string, 4> m_aLogTypes
const std::string m_strUnknownLogType;
HANDLE m_hConsoleOutput;
WORD m_consoleDefualtColor;
public:
explicit Logger( const std::string& strLogFilename );
virtual ~Logger();
static void log( const std::string& strText, LoggerType eLogType = TYPE_INFO );
static void log( const std::ostringstream& strStreamText, LoggerType eLogType = TYPE_INFO );
static void log( const char* szText, LoggerType eLogType = TYPE_INFO );
private:
Logger( const Logger& c ); // Not Implemented
Logger& operator=( const Logger& c ); // Not Implemented
}; // Logger
} // namespace pro
#endif // LOGGER_H
Logger.cpp
#include "stdafx.h"
#include "Logger.h"
#include "BlockThread.h"
#include "TextFileWriter.h"
namespace pro {
static Logger* s_pLogger = nullptr;
static CRITICAL_SECTION = s_criticalSection;
// White Text On Red Background
static const WORD WHITE_ON_RED = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED;
Logger::Logger( const std::string& strLogFilename ) :
Singleton( TYPE_LOGGER ),
m_strLogFilename( strLogFilename ),
m_uMaxCharacterLength( 0 ),
m_strUnknownLogType( "UNKNOWN" ) {
// Order Must Match Types Defined In Logger::Type enum
m_aLogTypes[0] = "Info";
m_aLogTypes[1] = "Warning";
m_aLogTypes[2] = "Error";
m_aLogTypes[3] = ""; // Console
// Find Widest Log Type String
m_uMaxCharacterLength = m_strUnknownLogType.size();
for each( const std::string& strLogType in m_aLogTypes ) {
if ( m_uMaxCharacterLength < strLogType.size() ) {
m_uMaxCharacterLength = strLogType.size();
}
}
InitializeCriticalSection( &s_criticalSection );
BlockThread blockThread( s_criticalSection ); // Enter Critical Section
// Start Log File
TextFileWriter file( m_strLogFilename, false, false );
// Prepare Console
m_hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE );
CONSOLE_SCREEN_BUFFER consoleInfo;
GetConsoleScreenBufferInfo( m_hConsoleOutput, &consoleInfo );
m_consoleDefaultColor = consoleInfo.wAttributes;
s_pLogger = this;
logMemoryAllocation( true );
}
Logger::~Logger() {
logMemoryAllocation( false );
s_pLogger = nullptr;
DeleteCriticalSection( &s_criticalSection );
}
void Logger::log( const std::string& strtext, LoggerType eLogType ) {
log( strText.c_str(), eLogType );
}
void Logger::log( const std::string& strText, LoggerType eLogType ) {
log( strText.str().c_str(), eLogType );
}
void Logger::log( const char* szText, LoggerType eLogType ) {
if ( nullptr == s_pLogger ) {
std::cout << "Logger has not been initialized, can not log " << szText << std::endl;
return;
}
BlockThread blockThread( s_criticalSection ); // Enter Critical Section
std::ostringstream strStream;
// Default White Text On Red Background
WORD textColor = WHITE_ON_RED;
// Chose Log Type Text String, Display "UNKNOWN" If eLogType Is Out Of Range
strStream << std::setfill(' ') << std::setw( s_pLogger->m_uMaxCharacterLength );
try {
if ( TYPE_CONSOLE != eLogType ) {
strStream << s_pLogger->m_aLogTypes.at( eLogType );
}
if ( TYPE_WARNING == eLogType ) {
// Yellow
textColor = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN;
} else if ( TYPE_INFO == eLogType ) {
// Green
textColor = FOREGROUND_GREEN;
} else if ( TYPE_CONSOLE == eLogType ) {
// Cyan
textColor = FOREGROUND_GREEN | FOREGROUND_BLUE;
}
} catch( ... ) {
strStream << s_pLogger->m_strUnknownLogType;
}
// Date And Time
if ( TYPE_CONSOLE != eLogType ) {
SYSTEMTIME time;
GetLocalTime( &time );
strStream << " [" << time.wYear << "."
<< std::setfill('0') << std::setw( 2 ) << time.wMonth << "."
<< std::setfill('0') << std::setw( 2 ) << time.wDay << " "
<< std::setfill(' ') << std::setw( 2 ) << time.wHour << ":"
<< std::setfill('0') << std::setw( 2 ) << time.wMinute << ":"
<< std::setfill('0') << std::setw( 2 ) << time.wSecond << "."
<< std::setfill('0') << std::setw( 3 ) << time.wMilliseconds << "] ";
}
strStream << szText << std::endl;
// Log Message
SetConsoleTextAttribute( s_pLogger->m_hConsoleOutput, textColor );
std::cout << strStream.str();
// Save Message To Log File
try {
TextFileWriter file( s_pLogger->m_strLogFilename, true, false );
file.write( strStream.str() );
} catch( ... ) {
// Not Saved In Log File, Write Message To Console
std::cout << __FUNCTION__ << " failed to write to file: " << strStream.str() << std::endl;
}
// Reset To Default Color
SetConsoleTextAttribute( s_pLogger->m_hConsoleOutput, s_pLogger->m_consoleDefaultColor );
}
} // namespace pro
FileHandler.h - Base Class
#ifndef FILE_HANDLER_H
#define FILE_HANDLER_H
namespace pro {
// class AssetStorage; // Not Used Here
class FileHandler {
protected:
// static AssetStorage* m_pAssetStorage; // Not Used Here
std::fstream m_fileStream;
std::string m_strFilePath;
std::string m_strFilenameWithPath;
private:
bool m_bSaveExceptionInLog;
public:
virtual ~FileHandle();
protected:
FileHandler( const std::string& strFilename, bool bSaveExceptionInLog );
void throwError( const std::string& strMessage ) const;
void throwError( const std::ostringstream& strStreamMessage ) const;
bool getString( std::string& str, bool appendPath );
private:
FileHandler( const FileHandler& c ); // Not Implemented
FileHandler& operator=( const FileHandler& c ); // Not Implemented
}; // FileHandler
} // namespace pro
#endif // FILE_HANDLER_H
FileHandler.cpp
#include "stdafx.h"
#include "FileHandler.h"
// #include "AssetStorage.h" // Not Used Here
namespace pro {
// AssetStorage* FileHandler::m_pAssetStorage = nullptr; // Not Used Here
FileHandler::FileHandler( const std::string& strFilename, bool bSaveExceptionInLog ) :
m_bSaveExceptionInLog( bSaveExceptionInLog ),
m_strFilenameWithPath( strFilename ) {
/*if ( bSaveExceptionInLog && nullptr == m_pAssetStorage ) {
m_pAssetStorage = AssetStorage::get();
}*/ // Not Used Here
// Extract Path Info If It Exists
std::string::size_type lastIndex = strFilename.find_last_of( "/\\" );
if ( lastIndex != std::string::npos ) {
m_strFilePath = strFilename.substr( 0, lastIndex );
}
if ( strFilename.empty() ) {
throw ExceptionHandler( __FUNCTION__ + std::string( " missing filename", m_bSaveExceptionInLog );
}
}
FileHandler::~FileHandler() {
if ( m_fileStream.is_open() ) {
m_fileStream.close();
}
}
void FileHandler::throwError( const std::string& strMessage ) const {
throw ExceptionHandler( "File [" + m_strFilenameWithPath + "] " + strMessage, m_bSaveExceptionInLog );
}
void FileHandler::throwError( const std::ostringstream& strStreamMessage ) const {
throwError( strStreamMessage.str() );
}
bool FileHandler::getString( std::string& str, bool appendPath ) {
m_fileStream.read( &str[0], str.size() );
if ( m_fileStream.fail() ) {
return false;
}
// Trim Right
str.erase( str.find_first_of( char( 0 ) ) );
if ( appendPath && !m_strFilePath.empty() ) {
// Add Path If One Exists
str = m_strFilePath + "/" + str;
}
return true;
}
} // namespace pro
Now for the two inherited classes that you have been waiting for to handle File Streams. These Two are Strictly Text. Others within my project are TextureFiles, ModelObjectFiles, etc. I will be showing only the TextFileReader & TextFileWriter.
TextFileReader.h
#ifndef TEXT_FILE_READER_H
#define TEXT_FILE_READER_H
#include "FileHandler.h"
namespace pro {
class TextFileReader : public FileHandler {
private:
public:
explicit TextFileReader( const std::string& strFilename );
// virtual ~ TextFileReader(); // Default Okay
std::string readAll() const;
bool readLine( std::string& strLine );
private:
TextFileReader( const TextFileReader& c ); // Not Implemented
TextFileReader& operator=( const TextFileReader& c ); // Not Implemented
}; // TextFileReader
} // namespace pro
#endif // TEXT_FILE_READER_H
TextFileReader.cpp
#include "stdafx.h"
#include "TextFileReader.h"
namespace pro {
TextFileReader::TextFileReader( const std::string& strFilename ) :
FileHandler( strFilename, true ) {
m_fileStream.open( m_strFilenameWithPath.c_str(), std::ios_base::in );
if ( !m_fileStream.is_open() ) {
throwError( __FUNCTION__ + std::string( " can not open file for reading" ) );
}
std::string TextFileReader::readAll() const {
std::ostringstream strStream;
strStream << m_fileStream.rdbuf();
return strStream.str();
}
bool TextFileReader::readLine( std::string& strLine ) {
if ( m_fileStream.eof() ) {
return false;
}
std::getline( m_fileStream, strLine );
return true;
}
} // namespace pro
TextFileWriter.h
#ifndef TEXT_FILE_WRITER_H
#define TEXT_FILE_WRITER_H
#include "FileHandler.h"
namespace pro {
class TextFileWriter : public FileHandler {
private:
public:
TextFileWriter( const std::string& strFilename, bool bAppendToFile, bool bSaveExceptionInLog = true );
void write( const std::string& str );
private:
TextFileWriter( const TextFileWriter& c ); // Not Implemented
TextFileWriter& operator=( const TextFileWriter& c ); // Not Implemented
}; // TextFileWriter
} // namespace pro
#endif // TEXT_FILE_WRITER_H
TextFileWriter.cpp
#include "stdafx.h"
#include "TextFileWriter.h"
namespace pro {
TextFileWriter::TextFileWriter( const std::string& strFilename, bool bAppendToFile, bool bSaveExceptionInLog ) :
FileHandler( strFilename, bSaveExceptionInLog ) {
m_fileStream.open( m_strFilenameWithPath.c_str(),
std::ios_base::out | ( bAppendToFile ? std::ios_base::app : std::ios_base::trunc ) );
if ( !m_fileStream.is_open() ) {
throwError( __FUNCTION__ + std::string( " can not open file for writing" ) );
}
}
void TextFileWriter::write( const std::string& str ) {
m_fileStream << str;
}
} // namespace pro
Now to see a sample of this in action. If you look in the Logger class you will already see a use of the TextFileWriter.
main.cpp
#include "stdafx.h"
#include "Logger.h"
#include "TextFileReader.h"
#include "TextFileWriter.h"
int _tmain( int iNumArguments, _TCHAR* pArgumentText[] ) {
using namespace pro;
try {
// This Is Using The TextFileWriter & Console Output
// Logger::TYPE_INFO is by default!
Logger logger( "logger.txt" );
logger.log( "Some Info" );
logger.log( "Error!", Logger::TYPE_ERROR );
logger.log( "Warning!", Logger::TYPE_WARNING );
TextFileReader textReaderSingle( "logger.txt" );
TextFileReader textReaderAll( "logger.txt" );
std::string strTextSingle;
std::string strTextAll;
textReaderSingle.readLine( strTextSingle );
std::cout << "Read Single Line: << std::endl << strText << std::endl << std::endl;
strTextAll = textReaderAll.readAll();
std::cout << "Read All: " << std::endl << strTextAll << std::endl;
//Check The logger.txt that was generated in your projects folder!
std::cout << "Press any key to quit" << std::endl;
_getch();
} catch ( ExceptionHandler& e ) {
std::cout << "Exception Thrown: " << e.getMessage() << std::endl;
std::cout << "Press any key to quit" << std::endl;
_getch();
return RETURN_ERROR;
} catch( ... ) {
std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
std::cout << "Press any key to quit" << std::endl;
_getch();
return RETURN_ERROR;
}
return RETURN_OK;
}
A majority of this work is accredited to Marek A. Krzeminski, MASc at www.MarekKnows.com. In essence all of these class objects are his; the only major difference is I used my own namespace pro as opposed to his. Both main functions are of my own work, the first stand alone and the second using his library code.
This is a project that has been in the works for a few years now and most of my advanced knowledge of the C++ language is due to following his video tutorials. This current project is a fairly large scale professional quality GameEngine using Shaders in OpenGL. All of this has been typed and debugged by hand while following along his tutorials.
As a major note; I had also hand typed a majority of this here as well, if this does not compile correctly it may be due to typographical errors. The source it self is a working application. What you see here is a very small percentage of his works! I am willing to accept the credit as to answer this person's question with the accumulation of this knowledge but I can not accept credit for this as being my own work in order to protect Marek and his copyright materials.
With this kind of setup it is quite easy to create your own file parsers and multiple file parsers for different types of files. As I stated above there are two other classes inherited from FileHandler that I did not show. If you would like to see more of this project, please vist www.MarekKnows.com and join the community.

Related

How to avoid using exceptions, as a form of control flow, to jump through through deeply nested function calls?

I've currently been using custom exceptions to achieve the goal of jumping through deeply nested function calls, to get to a specific function in the call chain. For example, consider the following code:
#include <iostream>
struct label {};
void B();
void C();
void D();
void A() {
return B();
}
void B() { // I want to jump to the level of the B function in the call-chain.
try {
return C();
}
catch(const label& e) {
std::cout << "jumped to b function" << std::endl;
}
}
void C() {
return D();
}
void D() {
throw label();
}
int main() {
A();
return 0;
}
Note however that the above example is extremely contrived, and is simply for illustration purposes. In my actual code, I'm using this technique in a recursive-decent parser to recover from syntactical errors. Also note that I'm not using exceptions to jump around to different functions, like a glorified goto. I'm using the custom exception to always jump to one specific function near the top of the call chain.
The above code does work fine, but reading some of the top posts on the question Are exceptions as control flow considered a serious antipattern? If so, Why? (on the Software Engineering site), suggested that using exceptions in such a manner as the above scenario does, is consider an anti-pattern, and there are usually better was to accomplish one's goal.
Is my usage of a custom exception above appropriate? If not, what is a more reasonable way to accomplish my goal while avoiding using exactions as a form control flow? (Also, although I tagged this question as c++ since that's what I'm writing my parser in, I suppose this is a more language-agnostic question.)
Part 1: - Nested Function Stack Calls With Exceptions.
This may not fit your particular or exact needs, however I'm willing to share this example as I think that it may provide some insight and that it is related to your current situation.
I have a set of classes that are integrated together that handle multiple common tasks. The following set of classes include BlockProcess, BlockThread, FileHandlers, ExceptionHandler, Logger and a Utility class. There are several files here and please keep in mind that this light weight project is targeted towards Windows and that I am using Visual Studio 2017 with pre compiled headers.
I'm sure one can strip out any windows dependent code easily and replace with their equivalent system, architecture & environment includes and functionality.
I am also using a namespace called demo that wraps all the classes & functions in this small project; any user should replace this namespace with their own namespace name.
The main purpose of this is the design process of how I typically handle exceptions when the stack calls are nested quite deep.
These sets of classes not only allow control of Logging information, warnings & errors to the console with different settings for different types of messages, but also gives the ability to log the contents to a file.
This type of construct is very handy and versatile while being in the process of developing 3D Graphics Applications which can become very intense in their code base.
I can not take full credit for this code as a majority of this was inspired and designed by Marek A. Krzeminski, MASc which can be seen here yet I believe that it is the concepts and the use of this code that is important.
Main Entry Point:
main.cpp
#include "stdafx.h"
#include "BlockProcess.h"
#include "Logger.h"
#include "Utility.h"
//struct label {}; // Instead of throwing this struct in D() I'm throwing the ExceptionHandler
void B();
void C();
void D();
void A() {
return B();
}
void B() {
using namespace demo;
try {
return C();
} catch ( ... ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " failed for some reason.";
Logger::log( strStream, Logger::TYPE_INFO );
Logger::log( strStream, Logger::TYPE_WARNING );
Logger::log( strStream, Logger::TYPE_ERROR );
Logger::log( strStream, Logger::TYPE_CONSOLE );
}
}
void C() {
return D();
}
void D() {
using namespace demo;
std::ostringstream strStream;
strStream << __FUNCTION__ << " failed for some reason.";
throw ExceptionHandler( strStream ); // By Default will log to file; otherwise pass false for second param.
}
int _tmain( int iNumArgs, _TCHAR* pArugmentText[] ) {
using namespace demo;
try {
Logger log( "logger.txt" );
A();
// Prevent Multiple Start Ups Of This Application
BlockProcess processBlock( "ExceptionManager.exe" );
if ( processBlock.isBlocked() ) {
std::ostringstream strStream;
strStream << "ExceptionManager is already running in another window." << std::endl;
throw ExceptionHandler( strStream, false );
}
Utility::pressAnyKeyToQuit();
} catch ( ExceptionHandler& e ) {
std::cout << "Exception Thrown: " << e.getMessage() << std::endl;
Utility::pressAnyKeyToQuit();
return RETURN_ERROR;
} catch ( ... ) {
std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
Utility::pressAnyKeyToQuit();
return RETURN_ERROR;
}
return RETURN_OK;
}
As you can see from the images above I was able to generate a log file of the info, warnings, errors etc., and if you look at the 2 consoles they were running simultaneously and the 2nd or lower cmd window is throwing the exception since I used the BlockProcess class to manage only a single instance of this running application. This is a very versatile design. The messages or thrown errors are being generated.
Now if you do not want execution to stop because of a specific value of a variable, the return of a function, if statement etc. instead of throwing an ExceptionHandler you can easily just create an ostringstream object, populate it with the needed iformation and you can pass that to Logger with the default option of saving to the Log file turned on or passing false as the last param. You can even set what type of message through the logger's types.
So to answer your question if this is anti-pattern? I honestly do not think it is if you carefully design your project and know where & when to throw messages.
ExceptionHandler:
ExceptionHandler.h
#ifndef EXCEPTION_HANDLER_H
#define EXCEPTION_HANDLER_H
namespace demo {
class ExceptionHandler final {
private:
std::string strMessage_;
public:
explicit ExceptionHandler( const std::string& strMessage, bool bSaveInLog = true );
explicit ExceptionHandler( const std::ostringstream& strStreamMessage, bool bSaveInLog = true );
~ExceptionHandler() = default;
ExceptionHandler( const ExceptionHandler& c ) = default;
const std::string& getMessage() const;
ExceptionHandler& operator=( const ExceptionHandler& c ) = delete;
};
} // namespace demo
#endif // !EXCEPTION_HANDLER_H
ExceptionHandler.cpp
#include "stdafx.h"
#include "ExceptionHandler.h"
#include "Logger.h"
namespace demo {
ExceptionHandler::ExceptionHandler( const std::string& strMessage, bool bSaveInLog ) :
strMessage_( strMessage ) {
if ( bSaveInLog ) {
Logger::log( strMessage_, Logger::TYPE_ERROR );
}
}
ExceptionHandler::ExceptionHandler( const std::ostringstream& strStreamMessage, bool bSaveInLog ) :
strMessage_( strStreamMessage.str() ) {
if ( bSaveInLog ) {
Logger::log( strMessage_, Logger::TYPE_ERROR );
}
}
const std::string& ExceptionHandler::getMessage() const {
return strMessage_;
}
} // namespace demo
Logger:
Logger.h
#ifndef LOGGER_H
#define LOGGER_H
#include "Singleton.h"
namespace demo {
class Logger final : public Singleton {
public:
enum LoggerType {
TYPE_INFO = 0,
TYPE_WARNING,
TYPE_ERROR,
TYPE_CONSOLE,
}; // LoggerType
private:
std::string strLogFilename_;
unsigned uMaxCharacterLength_;
std::array<std::string, 4> aLogTypes_;
const std::string strUnknownLogType_;
HANDLE hConsoleOutput_;
WORD consoleDefaultColor_;
public:
explicit Logger( const std::string& strLogFilename );
virtual ~Logger();
static void log( const std::string& strText, LoggerType eLogType = TYPE_INFO );
static void log( const std::ostringstream& strStreamText, LoggerType eLogType = TYPE_INFO );
static void log( const char* szText, LoggerType eLogType = TYPE_INFO );
Logger( const Logger& c ) = delete;
Logger& operator=( const Logger& c ) = delete;
};
} // namespace demo
#endif // !LOGGER_H
Logger.cpp
#include "stdafx.h"
#include "Logger.h"
#include "BlockThread.h"
#include "TextFileWriter.h"
namespace demo {
static Logger* s_pLogger = nullptr;
static CRITICAL_SECTION s_criticalSection;
static const WORD WHITE_ON_RED = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED; // White Text On Red Background
Logger::Logger( const std::string& strLogFilename ) :
Singleton( TYPE_LOGGER ),
strLogFilename_( strLogFilename ),
uMaxCharacterLength_( 0 ),
strUnknownLogType_( "UNKNOWN" ) {
// Oder must match types defined in Logger::Type enum
aLogTypes_[0] = "Info";
aLogTypes_[1] = "Warning";
aLogTypes_[2] = "Error";
aLogTypes_[3] = ""; // Console
// Find widest log type string
uMaxCharacterLength_ = strUnknownLogType_.size();
for each ( const std::string& strLogType in aLogTypes_ ) {
if ( uMaxCharacterLength_ < strLogType.size() ) {
uMaxCharacterLength_ = strLogType.size();
}
}
InitializeCriticalSection( &s_criticalSection );
BlockThread blockThread( s_criticalSection ); // Enter critical section
// Start log file
TextFileWriter file( strLogFilename_, false, false );
// Prepare console
hConsoleOutput_ = GetStdHandle( STD_OUTPUT_HANDLE );
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
GetConsoleScreenBufferInfo( hConsoleOutput_, &consoleInfo );
consoleDefaultColor_ = consoleInfo.wAttributes;
s_pLogger = this;
logMemoryAllocation( true );
} // Logger()
Logger::~Logger() {
logMemoryAllocation( false );
s_pLogger = nullptr;
DeleteCriticalSection( &s_criticalSection );
} // ~Logger
void Logger::log( const std::string& strText, LoggerType eLogType ) {
log( strText.c_str(), eLogType );
}
void Logger::log( const std::ostringstream& strStreamText, LoggerType eLogType ) {
log( strStreamText.str().c_str(), eLogType );
}
void Logger::log( const char* szText, LoggerType eLogType ) {
if ( nullptr == s_pLogger ) {
std::cout << "Logger has not been initialized, can not log " << szText << std::endl;
return;
}
BlockThread blockThread( s_criticalSection ); // Enter critical section
std::ostringstream strStream;
// Default White Text On Red Background
WORD textColor = WHITE_ON_RED;
// Choose log type text string, display "UNKNOWN" if eLogType is out of range
strStream << std::setfill( ' ' ) << std::setw( s_pLogger->uMaxCharacterLength_ );
try {
if ( TYPE_CONSOLE != eLogType ) {
strStream << s_pLogger->aLogTypes_.at( eLogType );
}
if ( TYPE_WARNING == eLogType ) {
// Yellow
textColor = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN;
} else if ( TYPE_INFO == eLogType ) {
// Green
textColor = FOREGROUND_GREEN;
} else if ( TYPE_CONSOLE == eLogType ) {
// Cyan
textColor = FOREGROUND_GREEN | FOREGROUND_BLUE;
}
} catch ( ... ) {
strStream << s_pLogger->strUnknownLogType_;
}
// Date & Time
if ( TYPE_CONSOLE != eLogType ) {
SYSTEMTIME time;
GetLocalTime( &time );
strStream << " [" << time.wYear << "."
<< std::setfill( '0' ) << std::setw( 2 ) << time.wMonth << "."
<< std::setfill( '0' ) << std::setw( 2 ) << time.wDay << " "
<< std::setfill( ' ' ) << std::setw( 2 ) << time.wHour << ":"
<< std::setfill( '0' ) << std::setw( 2 ) << time.wMinute << ":"
<< std::setfill( '0' ) << std::setw( 2 ) << time.wSecond << "."
<< std::setfill( '0' ) << std::setw( 3 ) << time.wMilliseconds << "] ";
}
strStream << szText << std::endl;
// Log message
SetConsoleTextAttribute( s_pLogger->hConsoleOutput_, textColor );
std::cout << strStream.str();
// Save same message to file
try {
TextFileWriter file( s_pLogger->strLogFilename_, true, false );
file.write( strStream.str() );
} catch ( ... ) {
// Ignore, not saved in log file
std::cout << __FUNCTION__ << " failed to write to file: " << strStream.str() << std::endl;
}
// Reset to default color
SetConsoleTextAttribute( s_pLogger->hConsoleOutput_, s_pLogger->consoleDefaultColor_ );
}
} // namespace demo
Singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H
namespace demo {
class Singleton {
public:
// Number of items in enum type must match the number of items and order of items stored in s_aSingletons
enum SingletonType {
TYPE_LOGGER = 0, // MUST BE FIRST!
}; // enum SingleType
private:
SingletonType eType_;
public:
Singleton( const Singleton& c ) = delete;
Singleton& operator=( const Singleton& c ) = delete;
virtual ~Singleton();
protected:
explicit Singleton( SingletonType eType );
void logMemoryAllocation( bool isAllocated ) const;
};
} // namespace demo
#endif // !SINGLETON_H
Singleton.cpp
#include "stdafx.h"
#include "Singleton.h"
#include "Logger.h"
namespace demo {
struct SingletonInfo {
const std::string strSingletonName;
bool isConstructed;
SingletonInfo( const std::string& strSingletonNameIn ) :
strSingletonName( strSingletonNameIn ),
isConstructed( false )
{}
};
// Order must match types defined in Singleton::SingletonType enum
static std::array<SingletonInfo, 1> s_aSingletons = { SingletonInfo( "Logger" ) };
Singleton::Singleton( SingletonType eType ) :
eType_( eType ) {
bool bSaveInLog = s_aSingletons.at( TYPE_LOGGER ).isConstructed;
try {
if ( !s_aSingletons.at( eType ).isConstructed ) {
// Test Initialize Order
for ( int i = 0; i < eType; ++i ) {
if ( !s_aSingletons.at( i ).isConstructed ) {
throw ExceptionHandler( s_aSingletons.at( i ).strSingletonName +
" must be constructed before constructing " +
s_aSingletons.at( eType ).strSingletonName,
bSaveInLog );
}
}
s_aSingletons.at( eType ).isConstructed = true;
} else {
throw ExceptionHandler( s_aSingletons.at( eType ).strSingletonName +
" can only be constructed once.",
bSaveInLog );
}
} catch ( std::exception& ) {
// eType is out of range
std::ostringstream strStream;
strStream << __FUNCTION__ << " Invalid Singleton Type specified: " << eType;
throw ExceptionHandler( strStream, bSaveInLog );
}
}
Singleton::~Singleton() {
s_aSingletons.at( eType_ ).isConstructed = false;
}
void Singleton::logMemoryAllocation( bool isAllocated ) const {
if ( isAllocated ) {
Logger::log( "Created " + s_aSingletons.at( eType_ ).strSingletonName );
} else {
Logger::log( "Destroyed " + s_aSingletons.at( eType_ ).strSingletonName );
}
}
} // namespace demo
For the reset of the project code see the 2nd provided answer: If you are looking to up-vote or accept please use this as the primary answer to vote upon, and please do not vote on the 2nd answer as it is only reference to this answer!
Part 2: - Nested Function Stack Calls With Exceptions.
Note: - Please do not VOTE on this answer please refer to the first answer as this is just a continuation for reference of pertaining classes!
You can find Part 1 here. I had to split this into 2 separate answers for I was about 2,000 characters above the max character limit of 30,000. I do apologize for any inconvenience. However one can not apply this ExceptionHandler without the provided classes.
FileHandlers:
FileHandler.h
#ifndef FILE_HANDLER_H
#define FILE_HANDLER_H
namespace demo {
class FileHandler {
protected:
std::fstream fileStream_;
std::string strFilePath_;
std::string strFilenameWithPath_;
private:
bool bSaveExceptionInLog_;
public:
virtual ~FileHandler();
FileHandler( const FileHandler& c ) = delete;
FileHandler& operator=( const FileHandler& c ) = delete;
protected:
FileHandler( const std::string& strFilename, bool bSaveExceptionInLog );
void throwError( const std::string& strMessage ) const;
void throwError( const std::ostringstream& strStreamMessage ) const;
bool getString( std::string& str, bool appendPath );
};
} // namespace demo
#endif // !FILE_HANDLER_H
FileHandler.cpp
#include "stdafx.h"
#include "FileHandler.h"
namespace demo {
FileHandler::FileHandler( const std::string& strFilename, bool bSaveExceptionInLog ) :
bSaveExceptionInLog_( bSaveExceptionInLog ),
strFilenameWithPath_( strFilename ) {
// Extract path info if it exists
std::string::size_type lastIndex = strFilename.find_last_of( "/\\" );
if ( lastIndex != std::string::npos ) {
strFilePath_ = strFilename.substr( 0, lastIndex );
}
if ( strFilename.empty() ) {
throw ExceptionHandler( __FUNCTION__ + std::string( " missing filename", bSaveExceptionInLog_ ) );
}
}
FileHandler::~FileHandler() {
if ( fileStream_.is_open() ) {
fileStream_.close();
}
}
void FileHandler::throwError( const std::string& strMessage ) const {
throw ExceptionHandler( "File [" + strFilenameWithPath_ + "] " + strMessage, bSaveExceptionInLog_ );
}
void FileHandler::throwError( const std::ostringstream& strStreamMessage ) const {
throwError( strStreamMessage.str() );
}
bool FileHandler::getString( std::string& str, bool appendPath ) {
fileStream_.read( &str[0], str.size() );
if ( fileStream_.fail() ) {
return false;
}
// Trim Right
str.erase( str.find_first_of( char( 0 ) ) );
if ( appendPath && !strFilePath_.empty() ) {
// Add path if one exists
str = strFilePath_ + "/" + str;
}
return true;
}
} // namespace demo
TextFileReader.h
#ifndef TEXT_FILE_READER_H
#define TEXT_FILE_READER_H
#include "FileHandler.h"
namespace demo {
class TextFileReader : public FileHandler {
public:
explicit TextFileReader( const std::string& strFilename );
virtual ~TextFileReader() = default;
std::string readAll() const;
bool readLine( std::string& strLine );
TextFileReader( const TextFileReader& c ) = delete;
TextFileReader& operator=( const TextFileReader& c ) = delete;
};
} // namespace demo
#endif // !TEXT_FILE_READER_H
TextFileReader.cpp
#include "stdafx.h"
#include "TextFileReader.h"
namespace demo {
TextFileReader::TextFileReader( const std::string& strFilename ) :
FileHandler( strFilename, true ) {
fileStream_.open( strFilenameWithPath_.c_str(), std::ios_base::in );
if ( !fileStream_.is_open() ) {
throwError( __FUNCTION__ + std::string( " can not open file for reading" ) );
}
}
std::string TextFileReader::readAll() const {
std::ostringstream strStream;
strStream << fileStream_.rdbuf();
return strStream.str();
}
bool TextFileReader::readLine( std::string& strLine ) {
if ( fileStream_.eof() ) {
return false;
}
std::getline( fileStream_, strLine );
return true;
}
} // namespace demo
TextFileWriter.h
#ifndef TEXT_FILE_WRITER_H
#define TEXT_FILE_WRITER_H
#include "FileHandler.h"
namespace demo {
class TextFileWriter : public FileHandler {
public:
explicit TextFileWriter( const std::string& strFilename, bool bAppendToFile, bool bSaveExceptionInLog = true );
virtual ~TextFileWriter() = default;
void write( const std::string& str );
TextFileWriter( const TextFileWriter& c ) = delete;
TextFileWriter& operator=( const TextFileWriter& c ) = delete;
};
} // namespace demo
#endif // !TEXT_FILE_WRITER_H
TextFileWriter.cpp
#include "stdafx.h"
#include "TextFileWriter.h"
namespace demo {
TextFileWriter::TextFileWriter( const std::string& strFilename, bool bAppendToFile, bool bSaveExceptionInLog ) :
FileHandler( strFilename, bSaveExceptionInLog ) {
fileStream_.open( strFilenameWithPath_.c_str(),
std::ios_base::out | (bAppendToFile ? std::ios_base::app : std::ios_base::trunc) );
if ( !fileStream_.is_open() ) {
throwError( __FUNCTION__ + std::string( " can not open file for writing" ) );
}
}
void TextFileWriter::write( const std::string& str ) {
fileStream_ << str;
}
} // namespace demo
Processes & Threads
BlockProcess.h
#ifndef BLOCK_PROCESS_H
#define BLOCK_PROCESS_H
namespace demo {
class BlockProcess final {
private:
HANDLE hMutex_;
public:
explicit BlockProcess( const std::string& strName );
~BlockProcess();
bool isBlocked() const;
BlockProcess( const BlockProcess& c ) = delete;
BlockProcess& operator=( const BlockProcess& c ) = delete;
};
} // namespace demo
#endif // !BLOCK_PROCESS_H
BlockProccess.cpp
#include "stdafx.h"
#include "BlockProcess.h"
namespace demo {
BlockProcess::BlockProcess( const std::string& strName ) {
hMutex_ = CreateMutex( nullptr, FALSE, strName.c_str() );
}
BlockProcess::~BlockProcess() {
CloseHandle( hMutex_ );
}
bool BlockProcess::isBlocked() const {
return (hMutex_ == nullptr || GetLastError() == ERROR_ALREADY_EXISTS);
}
} // namespace demo
BlockThread.h
#ifndef BLOCK_THREAD_H
#define BLOCK_THREAD_H
namespace demo {
class BlockThread final {
private:
CRITICAL_SECTION* pCriticalSection_;
public:
explicit BlockThread( CRITICAL_SECTION& criticalSection );
~BlockThread();
BlockThread( const BlockThread& c ) = delete;
BlockThread& operator=( const BlockThread& c ) = delete;
};
} // namespace demo
#endif // !BLOCK_THREAD_H
BlockThread.cpp
#include "stdafx.h"
#include "BlockThread.h"
namespace demo {
BlockThread::BlockThread( CRITICAL_SECTION& criticalSection ) {
pCriticalSection_ = &criticalSection;
EnterCriticalSection( pCriticalSection_ );
}
BlockThread::~BlockThread() {
LeaveCriticalSection( pCriticalSection_ );
}
} // namespace demo
Utilities:
Utility.h
#ifndef UTILITY_H
#define UTILITY_H
namespace demo {
class Utility {
public:
static void pressAnyKeyToQuit();
static std::string toUpper( const std::string& str );
static std::string toLower( const std::string& str );
static std::string trim( const std::string& str, const std::string elementsToTrim = " \t\n\r" );
static unsigned convertToUnsigned( const std::string& str );
static int convertToInt( const std::string& str );
static float convertToFloat( const std::string& str );
static std::vector<std::string> splitString( const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty = true );
Utility( const Utility& c ) = delete;
Utility& operator=( const Utility& c ) = delete;
private:
Utility(); // Private - Not A Class Object
template<typename T>
static bool stringToValue( const std::string& str, T* pValue, unsigned uNumValues );
template<typename T>
static T getValue( const std::string& str, std::size_t& remainder );
};
#include "Utility.inl"
} // namespace demo
#endif // !UTILITY_H
Utility.inl
template<typename T>
static bool Utility::stringToValue( const std::string& str, T* pValue, unsigned uNumValues ) {
int numCommas = std::count( str.begin(), str.end(), ',' );
if ( numCommas != uNumValues - 1 ) {
return false;
}
std::size_t remainder;
pValue[0] = getValue<T>( str, remainder );
if ( uNumValues == 1 ) {
if ( str.size() != remainder ) {
return false;
}
} else {
std::size_t offset = remainder;
if ( str.at( offset ) != ',' ) {
return false;
}
unsigned uLastIdx = uNumValues - 1;
for ( unsigned u = 1; u < uNumValues; ++u ) {
pValue[u] = getValue<T>( str.substr( ++offset ), remainder );
offset += remainder;
if ( (u < uLastIdx && str.at( offset ) != ',') ||
(u == uLastIdx && offset != str.size()) ) {
return false;
}
}
}
return true;
}
Utility.cpp
#include "stdafx.h"
#include "Utility.h"
namespace demo {
void Utility::pressAnyKeyToQuit() {
std::cout << "\nPress any key to quit." << std::endl;
_getch();
}
std::string Utility::toUpper( const std::string& str ) {
std::string result = str;
std::transform( str.begin(), str.end(), result.begin(), ::toupper );
return result;
}
std::string Utility::toLower( const std::string& str ) {
std::string result = str;
std::transform( str.begin(), str.end(), result.begin(), ::tolower );
return result;
}
std::string Utility::trim( const std::string& str, const std::string elementsToTrim ) {
std::basic_string<char>::size_type firstIndex = str.find_first_not_of( elementsToTrim );
if ( firstIndex == std::string::npos ) {
return std::string(); // Nothing Left
}
std::basic_string<char>::size_type lastIndex = str.find_last_not_of( elementsToTrim );
return str.substr( firstIndex, lastIndex - firstIndex + 1 );
}
template<>
float Utility::getValue( const std::string& str, std::size_t& remainder ) {
return std::stof( str, &remainder );
}
template<>
int Utility::getValue( const std::string& str, std::size_t& remainder ) {
return std::stoi( str, &remainder );
}
template<>
unsigned Utility::getValue( const std::string& str, std::size_t& remainder ) {
return std::stoul( str, &remainder );
}
unsigned Utility::convertToUnsigned( const std::string& str ) {
unsigned u = 0;
if ( !stringToValue( str, &u, 1 ) ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to unsigned";
throw strStream.str();
}
return u;
}
int Utility::convertToInt( const std::string& str ) {
int i = 0;
if ( !stringToValue( str, &i, 1 ) ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to int";
throw strStream.str();
}
return i;
}
float Utility::convertToFloat( const std::string& str ) {
float f = 0;
if ( !stringToValue( str, &f, 1 ) ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to float";
throw strStream.str();
}
return f;
}
std::vector<std::string> Utility::splitString( const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty ) {
std::vector<std::string> vResult;
if ( strDelimiter.empty() ) {
vResult.push_back( strStringToSplit );
return vResult;
}
std::string::const_iterator itSubStrStart = strStringToSplit.begin(), itSubStrEnd;
while ( true ) {
itSubStrEnd = search( itSubStrStart, strStringToSplit.end(), strDelimiter.begin(), strDelimiter.end() );
std::string strTemp( itSubStrStart, itSubStrEnd );
if ( keepEmpty || !strTemp.empty() ) {
vResult.push_back( strTemp );
}
if ( itSubStrEnd == strStringToSplit.end() ) {
break;
}
itSubStrStart = itSubStrEnd + strDelimiter.size();
}
return vResult;
}
} // namespace demo
Precompiled Headers:
stdafx.h
#ifndef STDAFX_H
#define STDAFX_H
// Included files that typically will not change
// during the development process of this application.
// System - Architect Includes
#include <Windows.h>
#include <process.h>
//#include <mmsystem.h>
// Character & Basic IO
#include <conio.h> // for _getch()
#include <tchar.h>
//---------------------------------------------//
// Standard Library Includes
// Atomics, Regular Expressions, Localizations
#include <atomic> // C++11
#include <clocale>
//#include <codecvt> // C++11 // Deprecated in C++17
#include <locale>
#include <regex>
// Numerics & Numeric Limits
#include <climits>
#include <cfloat>
#include <cstdint> // C++11
#include <cinttypes> // C++11
#include <limits>
#include <cmath>
#include <complex>
#include <valarray>
#include <random> // C++11
#include <numeric>
#include <ratio> // C++11
#include <cfenv> // C++11
// Strings, Streams & IO
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <fstream>
// Thread Support
#include <thread> // C++11
#include <mutex> // C++11
#include <shared_mutex> // C++14
#include <future> // C++11
#include <condition_variable> // C++11
// Containers
#include <array> // C++11
#include <stack>
#include <list>
#include <forward_list> // C++11
#include <map>
#include <unordered_map> // C++11
#include <queue>
#include <deque>
#include <set>
#include <unordered_set> // C++11
#include <vector>
// Algorithms, Iterators
#include <algorithm> // Note* C++ 17 also has <execution>
#include <iterator>
// Dynamic Memory
#include <new>
#include <memory>
#include <scoped_allocator> // C++11
// Utilities
#include <bitset>
#include <ctime> // Compatability with C style time formarts
#include <chrono> // C++ 11 - C++ Time Utilities
#include <functional>
#include <initializer_list> // C++11
#include <memory>
#include <thread>
#include <typeinfo>
#include <typeindex> // C++11
#include <type_traits> // C++11
#include <tuple> // C++11
#include <utility>
// C++ 17
#include <any>
#include <filesystem>
#include <optional>
#include <string_view>
#include <variant>
// C++ 20
// #include <compare>
// #include <charconv>
// #include <syncstream>
// 3rd Party Library Includes Here.
// User-Application Specific commonly used non changing headers.
#include "ExceptionHandler.h"
namespace demo {
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
}; // ReturnCode
extern const unsigned INVALID_UNSIGNED;
extern const unsigned INVALID_UNSIGNED_SHORT;
} // namespace demo
#endif // !STDAFX_H
stdafx.cpp
#include "stdafx.h"
namespace demo {
const unsigned INVALID_UNSIGNED = static_cast<const unsigned>(-1);
const unsigned INVALID_UNSIGNED_SHORT = static_cast<const unsigned short>(-1);
} // namespace demo

c++ can an fstream infile be used to gather data from an recently used ofstream outfile?

After an outfile data is processed, I want to take in that data back in again using an fstream variable called infile. Can an operation like the one below be possible after the outfile is complete? My main goal is to read back in what was randomly put out to the file so I can process it into a linked list eventually. I have tested a code similar to this but I get zeroes as a result. Thanks.
ifstream infile;
string FileName = "Numbers.txt";
infile.open(FileName);
while (infile)
{
infile >> RandomNumber;
cout << RandomNumber << endl;
}
infile.close();
This should work if ( infile ) will check if the file is opened and while ( infile >> RandomNumber ) will get all the numbers in the file:
ifstream infile;
string FileName = "Numbers.txt";
infile.open(FileName);
if ( infile ) {
while ( infile >> RandomNumber ) {
cout << RandomNumber << endl;
}
}
infile.close();
This is not an answer to the OPs question but rather it is provided towards a small conversation within a series of comments about reading files and parsing data. This is directed towards Tony and any who would wish to use this class setup. There are some things with in this class that pertain to other classes within my library, some of these include my Logger which would log messages to either a file or the console, and the message types are either Info, Warning, Error or Console. My Logger is derived from a Singleton class since I would only need one logger per solution to handle all messages. I have a Utility Class that hands a majority of string manipulation functions where the constructor is private and all functions or members are declared as static. This class also relies on an ExceptionHandler class that will accept either std::strings or std::ostringstream objects. My Logger accepts the same. Also some of my classes also rely on a BlockThread class that allows to work with multithreading which upon construction it will Create and Initialize a CriticalSection then Enter the Critical Section and then upon Destruction it will Leave and then Destroy the CriticalSection. Now as for this demonstration I will only be showing my FileHandler Classes along with 2 of its derived types, TextFileReader and TextFileWriter. I have more derived FileHandlers that work with reading in texture files, custom data structures etc, that work on binary files as opposed to text file, but for the purpose of this only the Text File Handlers will be shown.
FileHandler.h
#ifndef FILE_HANDLER_H
#define FILE_HANDLER_H
namespace util {
//class AssetStorage;
class FileHandler {
protected:
//static AssetStorage* m_pAssetStorage;
std::fstream m_fileStream;
std::string m_strFilePath;
std::string m_strFilenameWithPath;
private:
bool m_bSaveExceptionInLog;
public:
virtual ~FileHandler();
protected:
FileHandler( const std::string& strFilename, bool bSaveExceptionInLog );
void throwError( const std::string& strMessage ) const;
void throwError( const std::ostringstream& strStreamMessage ) const;
bool getString( std::string& str, bool appendPath );
private:
FileHandler( const FileHandler& c ); // Not Implemented
FileHandler& operator=( const FileHandler& c ); // Not Implemented
}; // FileHandler
} // namespace util
FileHandler.cpp
#include "stdafx.h"
#include "FileHandler.h"
namespace util {
// ----------------------------------------------------------------------------
// FileHandler()
FileHandler::FileHandler( const std::string& strFilename, bool bSaveExceptionInLog ) :
m_bSaveExceptionInLog( bSaveExceptionInLog ),
m_strFilenameWithPath( strFilename ) {
// Extract Path Info If It Exists
std::string::size_type lastIndex = strFilename.find_last_of( "/\\" );
if ( lastIndex != std::string::npos ) {
m_strFilePath = strFilename.substr( 0, lastIndex );
}
if ( strFilename.empty() ) {
throw ExceptionHandler( __FUNCTION__ + std::string( " missing filename", m_bSaveExceptionInLog ) );
}
} // FileHandler
// ----------------------------------------------------------------------------
// ~FileHandler
FileHandler::~FileHandler() {
if ( m_fileStream.is_open() ) {
m_fileStream.close();
}
} // ~FileHandler
// ----------------------------------------------------------------------------
// throwError()
void FileHandler::throwError( const std::string& strMessage ) const {
throw ExceptionHandler( "File [" + m_strFilenameWithPath + "] " + strMessage, m_bSaveExceptionInLog );
} // throwError( const std::string )
// ----------------------------------------------------------------------------
// throwError()
void FileHandler::throwError( const std::ostringstream& strStreamMessage ) const {
throwError( strStreamMessage.str() );
} // throwError( const std::ostringstream )
// ----------------------------------------------------------------------------
// getString()
bool FileHandler::getString( std::string& str, bool appendPath ) {
m_fileStream.read( &str[0], str.size() );
if ( m_fileStream.fail() ) {
return false;
}
// Trim Right
str.erase( str.find_first_of( char(0) ) );
if ( appendPath && !m_strFilePath.empty() ) {
// Add Path If One Exixsts
str = m_strFilePath + "/" + str;
}
return true;
} // getString
} // namespace util
TextFileReader.h
#ifndef TEXT_FILE_READER_H
#define TEXT_FILE_READER_H
#include "FileHandler.h"
namespace util {
class TextFileReader : public FileHandler {
private:
public:
explicit TextFileReader( const std::string& strFilename );
// virtual ~TextFileReader(); // Default OK
std::string readAll() const;
bool readLine( std::string& strLine );
private:
TextFileReader( const TextFileReader& c ); // Not Implemented
TextFileReader& operator=( const TextFileReader& c ); // Not Implemented
}; // TextFileReader
} // namespace util
#endif // TEXT_FILE_READER_H
TextFileReader.cpp
#include "stdafx.h"
#include "TextFileReader.h"
namespace util {
// ----------------------------------------------------------------------------
// TextFileReader()
TextFileReader::TextFileReader( const std::string& strFilename ) :
FileHandler( strFilename, true ) {
m_fileStream.open( m_strFilenameWithPath.c_str(), std::ios_base::in );
if ( !m_fileStream.is_open() ) {
throwError( __FUNCTION__ + std::string( " can not open file for reading" ) );
}
} // TextFileReader
// ----------------------------------------------------------------------------
// readAll()
std::string TextFileReader::readAll() const {
std::ostringstream strStream;
strStream << m_fileStream.rdbuf();
return strStream.str();
} // readAll
// ----------------------------------------------------------------------------
// readLine()
// Returns A String Containing The Next Line Of Text Stored In The File
bool TextFileReader::readLine( std::string& strLine ) {
if ( m_fileStream.eof() ) {
return false;
}
std::getline( m_fileStream, strLine );
return true;
} // readLine
} // namespace util
TextFileWriter.h
#ifndef TEXT_FILE_WRITER_H
#define TEXT_FILE_WRITER_H
#include "FileHandler.h"
namespace util {
class TextFileWriter : public FileHandler {
private:
public:
TextFileWriter( const std::string& strFilename, bool bAppendToFile, bool bSaveExceptionInLog = true );
// virtual ~TextFileWriter(); // Default OK
void write( const std::string& str );
private:
TextFileWriter( const TextFileWriter& c ); // Not Implemented
TextFileWriter& operator=( const TextFileWriter& c ); // Not Implemented
}; // TextFileWriter
} // namespace util
#endif // TextFileWriter
TextFileWriter.cpp
#include "stdafx.h"
#include "TextFileWriter.h"
namespace util {
// ----------------------------------------------------------------------------
// TextFileWriter()
TextFileWriter::TextFileWriter( const std::string& strFilename, bool bAppendToFile, bool bSaveExceptionInLog ) :
FileHandler( strFilename, bSaveExceptionInLog ) {
m_fileStream.open( m_strFilenameWithPath.c_str(),
std::ios_base::out | (bAppendToFile ? std::ios_base::app : std::ios_base::trunc) );
if ( !m_fileStream.is_open() ) {
throwError( __FUNCTION__ + std::string( " can not open file for writing" ) );
}
} // TextFileWriter
// ----------------------------------------------------------------------------
// write()
void TextFileWriter::write( const std::string& str ) {
m_fileStream << str;
} // write
} // namespace util
And Here is some code fragments of a class that used the FileHandlers.
Here is the class header that uses the FileHandler It reads in a text file and parses it to determine which GUI objects to create, load into memory and how they are to be displayed on the screen, it will also nest one GUI type as a child to another. I will not be showing the full implementation of this class but only a few functions where it concerns the use of using the FileHandlers.
GuiLoader.h
#ifndef GUI_LOADER_H
#define GUI_LOADER_H
#include "Engine.h"
#include "CommonStructs.h"
#include "Property.h"
#include "TextFileReader.h"
namespace vmk {
class AssetStorage;
class GuiCompositeElement;
class GuiElement;
class GuiLayout;
class GuiRenderable;
class GuiText;
class VisualMko;
class GuiLoader sealed {
friend GuiElement* Engine::loadGui( const std::string& strFilename ) const;
private:
std::string m_strFilename;
util::TextFileReader m_file;
bool m_inBlockComment;
unsigned m_uNumBracesOpened;
unsigned m_uLineNumber; // Keep Track OfLine Number For Error Messages
GuiElement* m_pLastGuiRoot;
std::vector<GuiCompositeElement*> m_vpParents;
std::string m_strLine;
std::unordered_map<std::string, TextureInfo> m_mTextureInfos;
std::unordered_map<std::string, FontFace> m_mFontFace;
FontManager* m_pFontManager;
AssetStorage* m_pAssetStorage;
public:
// virtual ~GuiLoader(); // Default Ok
GuiElement* getRoot() const;
private:
GuiLoader( const std::string& strFilename );
GuiLoader( const GuiLoader& c ); // Not Implemented
GuiLoader& operator=( const GuiLoader& c ); // Not Implemented
bool getNextLine();
std::string getFailedLocation() const;
void removeComments();
void parseGui();
bool handleOpenBrace( unsigned uStartLocation, GuiCompositeElement* pGui );
void addToParent( GuiRenderable* pRenderable ) const;
bool getParameter( std::string strParam, unsigned uStartLocation, std::string& strValue, bool isRequired = true, char endCharacter = ' ' ) const;
void setOptionalParameters( unsigned uStartLocation, GuiElement* pGui ) const;
void getOptionalLayoutParameters( unsigned uStartLocation, glm::ivec2& offsetFromParent, Gui::Alignment& eAlignChildren, glm::uvec2& size, std::string& strId ) const;
void setLayoutParameters( unsigned uStartLocation, GuiLayout* pLayout ) const;
bool getOptionalBackgroundParameters( unsigned uStartLocation, TextureInfo& textureInfo, glm::uvec2& origin ) const;
bool getOptionalBackgroundParameters( unsigned uStartLocation, TextureInfo& textureInfo, glm::uvec2& origin, glm::uvec2& size, bool isRequired = true ) const;
void getRequiredTextParameters( unsigned uStartLocation, FontFace& fontFace, std::string& strText ) const;
void setTextShadowParameters( unsigned uStartLocation, GuiText* pGui ) const;
void setVisualMkoParameters( unsigned uStartLocation, VisualMko* pVisualMko ) const;
}; // GuiLoader
} // namespace vmk
#endif // GUI_LOADER_H
GuiLoader.cpp - Only A Few Portions Are Shown
#include "stdafx.h"
#include "GuiLoader.h"
#include "AssetStorage.h"
#include "FontManager.h"
#include "Gui.h"
#include "Image2d.h"
#include "Logger.h"
#include "TextureFileReader.h"
#include "Utility.h"
using namespace util;
namespace vmk {
// ----------------------------------------------------------------------------
// GuiLoader()
GuiLoader::GuiLoader( const std::string& strFilename ) :
m_strFilename( strFilename ),
m_file( strFilename ),
m_inBlockComment( false ),
m_uNumBracesOpened( 0 ),
m_uLineNumber( 0 ),
m_pLastGuiRoot( nullptr ),
m_pFontManager( FontManager::get() ),
m_pAssetStorage( AssetStorage::get() ) {
while ( getNextLine() ) {
parseGui();
}
if ( m_uNumBracesOpened > 0 ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << getFailedLocation() << ". Missing " << m_uNumBracesOpened << " closing brace" << ( m_uNumBracesOpened > 1 ? "s" : "" ) << ".";
throw ExceptionHandler( strStream );
}
if ( m_inBlockComment ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << getFailedLocation() << ". Missing closing block comment */.";
}
} // GuiLoader
// ----------------------------------------------------------------------------
// getRoot()
GuiElement* GuiLoader::getRoot() const {
return m_pLastGuiRoot;
} // getRoot
// ----------------------------------------------------------------------------
// getNextLine()
// Returns True If Got A Line Of Text (Could Be Blank If It Is All Commented
// Out Or If It Truly Was A Blank Line). False is Returned When There Is
// No More Data In The File
bool GuiLoader::getNextLine() {
if ( !m_file.readLine( m_strLine ) ) {
return false;
}
++m_uLineNumber;
m_strLine = Utility::trim( m_strLine );
//std::cout << m_uLineNumber << ": " << m_strLine << std::endl; // Use This For Debugging The GuiLoader
removeComments();
return true;
} // getNextLine
// ... Other Functions Here
} // namespace vmk
This was presented to show the robustness of my file handling classes. There are many other classes not shown here. My library is a part of a 3D Graphics Rendering Library that uses Modern OpenGL. There are several hundred class objects and 100,000s of lines of code for doing 3D graphics rendering, sprite loading, physics simulation, animation, audio playback, audio stream playback, and much more. Not all of this source code is of my own design for this code is copy right protected by Marek A. Krzeminski, MASc and his works can be found at www.MarekKnows.com
All code shown here was not copied and pasted from his site, it was all hand typed while following along with his video tutorials. It was compiled and debug manually. This project has been in the works for a couple of years, and still to this day more is being added. I have been a proud member of his website and community since 2007-2008.
int number = 0;
string filename( "Numbers.txt" );
std::ofstream out;
std::ifstream in;
std::cout << "enter a number: << std::endl;
std::cin >> number;
out.open( filename.c_str(), std::ios_base::out );
if ( !out.is_open() ){
// Print, Log Error, Throw Error, Return Etc.
}
for ( int i = 0; i < number; i++ ) {
out << rand() % 100 << std::endl;
}
out.close();
in.open( filename.c_str(), std::ios_base::in );
if ( !in.is_open() ) {
// Error Case Here
}
while ( !in.eof() ) { // Usually bad practice; but my file handling classes are parser objects are too large to show here.
in >> RandomNumber;
std::cout << RandomNumber << endl;
}
infile.close();

C++ Logger Runtime Threshold

namespace Log {
#include <ctime>
#include <string>
#include <boost\scoped_ptr.hpp>
#ifndef LOG_PREPEND_TIMESTAMP_DEFAULT
#define LOG_PREPEND_TIMESTAMP_DEFAULT false
#endif
enum LogLevel_t { logFatal = 0, logError = 1, logWarning = 2, logVerbose = 3, logDebug = 4 };
std::string LogLevelToString( const LogLevel_t level ) {
static const char* const buffer[] = { "Fatal", "Error", "Warning", "Verbose", "Debug" };
return buffer[level];
}
class Log: public boost::noncopyable {
protected:
boost::scoped_ptr< std::ostringstream > Output_String;
bool m_Prepending_Timestamp;
size_t m_LinesOutputted;
public:
Log():
Output_String( new std::ostringstream ),
m_Prepending_Timestamp( LOG_PREPEND_TIMESTAMP_DEFAULT ),
m_LinesOutputted( 0 ) {}
void UsingTimestamp( bool Prepending_Timestamp = true ) {
m_Prepending_Timestamp = Prepending_Timestamp;
}
std::ostringstream& Get( const LogLevel_t level ) {
// Write line number
++m_LinesOutputted;
*Output_String << m_LinesOutputted << " | ";
if( m_Prepending_Timestamp == true ) {
//prepare a timestamp
time_t now = time( NULL );
std::string formatted_time( asctime( localtime( &now ) ) ); /* &now can be replaced with the above call when r-value references work (maybe) saving a stack var */
formatted_time.erase(( formatted_time.length( ) - 1 ), 1 ); /* Removes the \n(newline) that asctime adds for better formatting */
// Write timestamp to stream
*Output_String << formatted_time << " || ";
}
// Write Logging level(severity) to stream
*Output_String << LogLevelToString( level ) << " || ";
return *Output_String;
}
void Flush() {
*Output_String << std::endl;
fprintf( stdout, "%s", Output_String->str( ).c_str( ) );
fflush( stdout );
Output_String.reset( new std::ostringstream ); /* streams have lots of internal state clean streams are good */
}
~Log() {
*Output_String << std::endl;
fprintf( stdout, "%s", Output_String->str( ).c_str( ) );
fflush( stdout );
}
};
How would I implement a threshold without a macro?
If I wrap the body of get with a check I still have to return the stringstream.
I could change the return type to a pointer instead of a reference then return NULL but then every logger statement would have to have a null check for the returned stringstream

Do not show console window in release but show in debug

I would like to create a program that will run in background as a final product. For debug purpose I want it to display a console.
I learned that there is a ShowWindow( hWnd, SW_HIDE ); function, but if I use it in a 'standard' main function the console window still pops up for a moment. I was trying to work it out like this (yes I know it's crappy):
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#define DEBUG
//#undef DEBUG
#ifndef DEBUG
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );
while(1);
return 0;
}
#else
#pragma comment(linker, "/SUBSYSTEM:CONSOLE")
int main(int argc, int **argv)
{
HWND hWnd = GetConsoleWindow();
while(1);
return 0;
}
#endif
Here I managed to prevent the window form popping up, but I can't pass parameters to the program.
I believe there is a much better solution for this. Can you share?
PS
I don't want to use .NET.
This is an answer to the first part of the question, "Here I managed to prevent the window form popping up", i.e. how to set the Windows subsystem for an application in Visual C++.
I will answer the second part of the question, about command line arguments, separately.
// How to create a Windows GUI or console subsystem app with a standard `main`.
#ifndef _MSC_VER
# error Hey, this is Visual C++ specific source code!
#endif
// Better set this in the project settings, so that it's more easily configured.
#ifdef NDEBUG
# pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )
#else
# pragma comment( linker, "/subsystem:console" )
#endif
#undef UNICODE
#define UNICODE
#undef NOMINMAX
#define NOMINAX
#undef STRICT
#define STRICT
#include <windows.h>
int main()
{
MessageBox( 0, L"Hi!", L"This is the app!", MB_SETFOREGROUND );
}
The NDEBUG standard C++ macro is designed for suppressing the effect of standard assert, so it’s not required to be globally meaningful. However, in practice it is globally meaningful. And then it provides a bit of portability compared to using a Visual C++ macro such as DEBUG.
Anyway, in order to make it easier to configure the subsystem, unless you want to enforce that debug builds should be console and release builds should be GUI, then I recommend doing this in the project settings rather than via a #pragma (note also that e.g. the g++ compiler does not support linker pragmas, so using the project settings is more portable).
If you want you can check the subsystem programmatically instead of just by inspection (i.e. instead of noting whether the above program produces a console or not):
// How to create a Windows GUI or console subsystem app with a standard `main`.
#ifndef _MSC_VER
# error Hey, this is Visual C++ specific source code!
#endif
// Better set this in the project settings, so that it's more easily configured.
#ifdef NDEBUG
# pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )
#else
# pragma comment( linker, "/subsystem:console" )
#endif
#undef UNICODE
#define UNICODE
#undef NOMINMAX
#define NOMINAX
#undef STRICT
#define STRICT
#include <windows.h>
#include <assert.h> // assert
#include <string> // std::wstring
#include <sstream> // std::wostringstream
using namespace std;
template< class Type >
wstring stringFrom( Type const& v )
{
wostringstream stream;
stream << v;
return stream.str();
}
class S
{
private:
wstring s_;
public:
template< class Type >
S& operator<<( Type const& v )
{
s_ += stringFrom( v );
return *this;
}
operator wstring const& () const { return s_; }
operator wchar_t const* () const { return s_.c_str(); }
};
IMAGE_NT_HEADERS const& imageHeaderRef()
{
HMODULE const hInstance =
GetModuleHandle( nullptr );
IMAGE_DOS_HEADER const* const pImageHeader =
reinterpret_cast< IMAGE_DOS_HEADER const* >( hInstance );
assert( pImageHeader->e_magic == IMAGE_DOS_SIGNATURE ); // "MZ"
IMAGE_NT_HEADERS const* const pNTHeaders = reinterpret_cast<IMAGE_NT_HEADERS const*>(
reinterpret_cast< char const* >( pImageHeader ) + pImageHeader->e_lfanew
);
assert( pNTHeaders->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC ); // "PE"
return *pNTHeaders;
}
int main()
{
IMAGE_NT_HEADERS const& imageHeader = imageHeaderRef();
WORD const subsystem = imageHeader.OptionalHeader.Subsystem;
MessageBox(
0,
S() << L"Subsystem " << subsystem << L" "
<< (0?0
: subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI? L"GUI"
: subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI? L"Console"
: L"Other"),
L"Subsystem info:",
MB_SETFOREGROUND );
}
You can still pass parameters to a regular, non-console Win32 program: they just show up in the single lpCmdLine string, all globbed together into one big command line. You can use CommandLineToArgvW to parse that into separate arguments, but do note that that function is only available in the Unicode flavor. For example:
int wmain(int argc, wchar_t **argv)
{
// Common main function (Unicode args)
}
#ifndef DEBUG
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Forward invocation to wmain
int argc;
LPWSTR *argv = CommandLineToArgvW(pCmdLine, &argc);
int status = wmain(argc, argv);
LocalFree(argv);
return status;
}
#endif
I'd also recommend using your project settings to set the executable's subsystem (console or Windows) depending on the configuration instead of using a #pragma to do it.
This is an answer to the second part of the question, “but I can't pass parameters to the program”, i.e. how to obtain the command line arguments in a Visual C++ Windows app.
The simplest but also most limited way is to use the arguments of a standard C++ main,
int main( int argc, char* argv[] )
{
// Whatever, e.g.
vector<string> const args( argv, argv + argc );
}
The C++ standard strongly suggests that those arguments should be encoded with some multi-byte character set such as UTF-8,
C++11 §3.6.1/2:
“If argc is nonzero these arguments shall be supplied in argv[0]
through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs) (17.5.2.1.4.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "".”
However, at the time of the first C++ standard, in 1998, neither the *nix world convention, nor the Windows convention, was to do this. Instead the convention was to pass the arguments with some locale-specific character encoding. The Linux world almost immediately started a migration towards UTF-8, while Windows did not, so that still as of 2012 in Windows the standard main arguments are not sufficient to pass e.g. arbitrary filenames…
Happily, in Windows the command line that’s passed to the process, and that is available via the GetCommandLine API function, is UTF-16 encoded, which means that any filename (and indeed any text) can be passed.
On the third hand, the API function that provides a standard parsing of the command line, CommandLineToArgvW, has at least one sillybug, and maybe more… And presumably the non-standard Visual C++ Unicode C++ startup function wmain has arguments provided by that function. So for best results, until that’s been fixed one should use some proper home-brewed command line parsing, e.g. as illustrated in the program below (I just picked an ad-hoc “personal tool” program I made last week, it’s similar to the Windows 2000 Resource Kit’s timethis):
// A program to measure the execution time of another program.
// Based vaguely on Jeffrey Richter's "timep" program in
// the 2nd edition of "Win32 System Programming".
//
// Author: Alf P. Steinbach, 2012. License: Boost license 1.0.
#undef UNICODE
#define UNICODE
#undef STRICT
#define STRICT
#undef NOMINMAX
#define NOMINMAX
#include <windows.h>
#include <shlwapi.h> // PathGetCharType
#include <assert.h> // assert
#include <functional> // std::function
#include <iomanip> // set::setfill, std::setw
#include <iostream> // std::wcout, std::endl
#include <sstream> // std::wostringstream
#include <stddef.h> // ptrdiff_t
#include <stdexcept> // std::runtime_error, std::exception
#include <stdint.h> // int64_t
#include <string> // std::string
#include <type_traits> // std::is_fundamental
#include <utility> // std::move
using namespace std;
#if !defined( CPP_STATIC_ASSERT )
# define CPP_STATIC_ASSERT( e ) static_assert( e, #e )
#endif
#if !defined( CPP_NORETURN )
# define CPP_NORETURN [[noreturn]]
#endif
// MSVC workaround: "#define CPP_NORETURN __declspec( noreturn )"
// clang workaround: "#define CPP_NORETURN __attribute__(( noreturn ))"
namespace cpp {
namespace detail {
template< class Destination, class Source >
class ImplicitCast
{
public:
static Destination value( Source const v )
{
return static_cast<Destination>( v );
}
};
template< class Source >
class ImplicitCast< bool, Source >
{
public:
static bool value( Source const v )
{
return !!v; // Shuts up Visual C++ sillywarning about performance.
}
};
};
template< class Destination, class Source >
Destination implicitCast( Source const v )
{
CPP_STATIC_ASSERT( is_fundamental< Destination >::value );
CPP_STATIC_ASSERT( is_fundamental< Source >::value );
return detail::ImplicitCast< Destination, Source >::value( v );
}
typedef ptrdiff_t Size;
inline bool hopefully( bool const c ) { return c; }
inline CPP_NORETURN bool throwX( string const& s )
{
throw runtime_error( s );
}
inline CPP_NORETURN bool throwX( string const& s, exception const& reasonX )
{
throwX( s + "\n!Because - " + reasonX.what() );
}
class ScopeGuard
{
private:
function<void()> cleanup_;
ScopeGuard( ScopeGuard const& ); // No such.
ScopeGuard& operator=( ScopeGuard const& ); // No such.
public:
~ScopeGuard() { cleanup_(); }
ScopeGuard( function<void()> const cleanup )
: cleanup_( cleanup )
{}
};
class SubstringRef
{
private:
wchar_t const* start_;
wchar_t const* end_;
public:
Size length() const { return end_ - start_; }
wchar_t const* start() const { return start_; }
wchar_t const* end() const { return end_; }
SubstringRef( wchar_t const* start, wchar_t const* end )
: start_( start )
, end_( end )
{}
};
inline void skipWhitespace( wchar_t const*& p )
{
while( *p != L'\0' && iswspace( *p ) ) { ++p; }
}
inline wchar_t const* theAfterWhitespacePart( wchar_t const* p )
{
skipWhitespace( p );
return p;
}
inline void invert( bool& b ) { b = !b; }
} // namespace cpp
namespace winapi {
using cpp::hopefully;
using cpp::invert;
using cpp::Size;
using cpp::skipWhitespace;
using cpp::SubstringRef;
using cpp::theAfterWhitespacePart;
using cpp::throwX;
namespace raw {
typedef DWORD DWord;
typedef FILETIME FileTime;
typedef HANDLE Handle;
typedef PROCESS_INFORMATION ProcessInformation;
typedef SYSTEMTIME SystemTime;
typedef WORD Word;
} // namespace raw
// The following logic is mainly a workaround for a bug in CommandLineToArgvW.
// See [http://preview.tinyurl.com/CommandLineToArgvWBug].
inline SubstringRef nextArgumentIn( wchar_t const* const commandLine )
{
wchar_t const* p = commandLine;
skipWhitespace( p );
wchar_t const* const start = p;
bool isInQuotedPart = false;
while( *p != L'\0' && (isInQuotedPart || !iswspace( *p ) ) )
{
if( *p == L'\"' ) { invert( isInQuotedPart ); }
++p;
}
return SubstringRef( start, p );
}
// This corresponds essentially to the argument of wWinMain(...).
inline wchar_t const* commandLineArgPart()
{
SubstringRef const programSpec = nextArgumentIn( GetCommandLine() );
return theAfterWhitespacePart( programSpec.end() );
}
class ProcessInfo
{
private:
raw::ProcessInformation info_;
ProcessInfo( ProcessInfo const& ); // No such.
ProcessInfo& operator=( ProcessInfo const& ); // No such.
public:
raw::ProcessInformation& raw() { return info_; }
raw::Handle handle() const { return info_.hProcess; }
~ProcessInfo()
{
::CloseHandle( info_.hThread );
::CloseHandle( info_.hProcess );
}
ProcessInfo(): info_() {}
ProcessInfo( ProcessInfo&& other )
: info_( move( other.info_ ) )
{
other.info_ = raw::ProcessInformation(); // Zero.
}
};
inline ProcessInfo createProcess( wchar_t const commandLine[] )
{
STARTUPINFO startupInfo = { sizeof( startupInfo ) };
ProcessInfo processInfo;
wstring mutableCommandLine( commandLine );
mutableCommandLine += L'\0';
GetStartupInfo( &startupInfo );
bool const creationSucceeded = !!CreateProcess (
nullptr, // LPCTSTR lpApplicationName,
&mutableCommandLine[0], // LPTSTR lpCommandLine,
nullptr, // LPSECURITY_ATTRIBUTES lpProcessAttributes,
nullptr, // LPSECURITY_ATTRIBUTES lpThreadAttributes,
true, // BOOL bInheritHandles,
NORMAL_PRIORITY_CLASS, // DWORD dwCreationFlags,
nullptr, // LPVOID lpEnvironment,
nullptr, // LPCTSTR lpCurrentDirectory,
&startupInfo, // LPSTARTUPINFO lpStartupInfo,
&processInfo.raw() // LPPROCESS_INFORMATION lpProcessInformation
);
hopefully( creationSucceeded )
|| throwX( "winapi::createProcess: CreateProcess failed" );
return processInfo;
}
inline raw::Handle dup(
raw::Handle const h,
raw::DWord const desiredAccess,
bool inheritable = false
)
{
raw::Handle result = 0;
bool const wasDuplicated = !!DuplicateHandle(
GetCurrentProcess(), h,
GetCurrentProcess(), &result,
desiredAccess,
inheritable,
0 // options
);
hopefully( wasDuplicated )
|| throwX( "winapi::dup: DuplicateHandle failed" );
assert( result != 0 );
return result;
}
inline int64_t mSecsFromRelative( raw::FileTime const t )
{
ULARGE_INTEGER asLargeInt;
asLargeInt.u.HighPart = t.dwHighDateTime;
asLargeInt.u.LowPart = t.dwLowDateTime;
return asLargeInt.QuadPart/10000;
}
SubstringRef filenamePart( SubstringRef const& path )
{
wchar_t const* p = path.end();
while( p != path.start() && PathGetCharType( *p ) != GCT_SEPARATOR )
{
--p;
}
if( PathGetCharType( *p ) == GCT_SEPARATOR ) { ++p; }
return SubstringRef( p, path.end() );
}
} // namespace winapi
winapi::ProcessInfo createProcess( wchar_t const commandLine[], char const errMsg[] )
{
try{ return winapi::createProcess( commandLine ); }
catch( exception const& x ) { cpp::throwX( errMsg, x ); }
}
winapi::raw::Handle run( wchar_t const commandLine[] )
{
namespace raw = winapi::raw;
using cpp::hopefully;
using cpp::throwX;
using winapi::dup;
using winapi::ProcessInfo;
static char const* const createErrMsg = "Failed to create process";
ProcessInfo const process = createProcess( commandLine, createErrMsg );
// Early handle duplication ensures that one has the required rights.
raw::Handle const accessibleHandle =
dup( process.handle(), PROCESS_QUERY_INFORMATION | SYNCHRONIZE );
raw::DWord const waitResult = WaitForSingleObject( process.handle(), INFINITE );
hopefully( waitResult == WAIT_OBJECT_0 )
|| throwX( "Failed waiting for process termination." );
return accessibleHandle;
}
class Interval
{
private:
int hours_;
int minutes_;
int seconds_;
int milliseconds_;
public:
int msecs() const { return milliseconds_; }
int seconds() const { return seconds_; }
int minutes() const { return minutes_; }
int hours() const { return hours_; }
Interval( int msecs, int seconds = 0, int minutes = 0, int hours = 0 )
: milliseconds_( msecs )
, seconds_( seconds )
, minutes_( minutes )
, hours_( hours )
{
assert( unsigned( hours ) < 24 );
assert( unsigned( minutes ) < 60 );
assert( unsigned( seconds ) < 60 );
assert( unsigned( msecs ) < 1000 );
}
static Interval fromMSecs( int msecs )
{
int const totalSeconds = msecs / 1000;
int const totalMinutes = totalSeconds / 60;
int const totalHours = totalMinutes / 24;
return Interval(
msecs % 1000, totalSeconds % 60, totalMinutes %60, totalHours
);
}
};
wostream& operator<<( wostream& stream, Interval const& t )
{
wostringstream formatter;
formatter << setfill( L'0' );
formatter
<< setw( 2 ) << t.hours() << ":"
<< setw( 2 ) << t.minutes() << ":"
<< setw( 2 ) << t.seconds() << "."
<< setw( 3 ) << t.msecs();
return (stream << formatter.str());
}
string narrowStringFrom( cpp::SubstringRef const& s )
{
return string( s.start(), s.end() ); // Non-ANSI characters => garbage.
}
void cppMain()
{
namespace raw = winapi::raw;
using cpp::hopefully;
using cpp::implicitCast;
using cpp::ScopeGuard;
using cpp::SubstringRef;
using cpp::throwX;
using winapi::commandLineArgPart;
using winapi::filenamePart;
using winapi::mSecsFromRelative;
using winapi::nextArgumentIn;
SubstringRef const programSpec = nextArgumentIn( GetCommandLine() );
SubstringRef const programName = filenamePart( programSpec );
wchar_t const* const otherCommandLine = commandLineArgPart();
hopefully( nextArgumentIn( otherCommandLine ).length() > 0 )
|| throwX( "Usage: " + narrowStringFrom( programName ) + " command" );
raw::DWord const startMSecs = GetTickCount();
raw::Handle const finishedProcess = run( otherCommandLine );
raw::DWord const endMSecs = GetTickCount();
raw::DWord const realElapsedMSecs = endMSecs - startMSecs;
ScopeGuard const closingHandle( [=]() { CloseHandle( finishedProcess ); } );
Interval const realElapsedTime = Interval::fromMSecs( realElapsedMSecs );
static char const* const commandLineLabel = "Command line: ";
static char const* const rElapsedTimeLabel = "External elapsed time: ";
static char const* const pElapsedTimeLabel = "In-process elapsed time: ";
static char const* const kernelTimeLabel = "In-process kernel time: ";
static char const* const userTimeLabel = "In-process user time: ";
wclog << endl;
wclog << commandLineLabel << "[" << otherCommandLine << "]" << endl;
wclog << rElapsedTimeLabel << realElapsedTime << endl;
raw::FileTime creationTime;
raw::FileTime exitTime;
raw::FileTime kernelTime;
raw::FileTime userTime;
bool const timesWereObtained = !!GetProcessTimes(
finishedProcess, &creationTime, &exitTime, &kernelTime, &userTime
);
hopefully( timesWereObtained )
|| throwX( "cppMain: GetProcessTimes failed" );
int const elapsedTimeMSecs= implicitCast<int>(
mSecsFromRelative( exitTime ) - mSecsFromRelative( creationTime )
);
int const kernelTimeMSecs = implicitCast<int>( mSecsFromRelative( kernelTime ) );
int const userTimeMSecs = implicitCast<int>( mSecsFromRelative( userTime ) );
wclog << pElapsedTimeLabel << Interval::fromMSecs( elapsedTimeMSecs ) << endl;
wclog << kernelTimeLabel << Interval::fromMSecs( kernelTimeMSecs ) << endl;
wclog << userTimeLabel << Interval::fromMSecs( userTimeMSecs ) << endl;
}
int main()
{
try
{
cppMain();
return EXIT_SUCCESS;
}
catch( exception const& x )
{
wcerr << "!" << x.what() << endl;
}
return EXIT_FAILURE;
}
Rather than changing your subsystem mode depending on the build type, consider using AllocConsole to explicitly create a console for your process.

How to check for new files in the directory?

Given:
filesystem::path toDir("./");
ptime oldTime;
ptime now(second_clock::local_time());
How can I determine which files were created in the time period between oldTime and now?
The names of such "fresh" files should be streamed to cout.
Update:
Well based on given answer I made a small programm:
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <boost/timer.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
using namespace boost::filesystem;
using namespace boost::posix_time;
using namespace boost::local_time;
using namespace boost::gregorian;
using namespace std;
path file_service_default_path;
boost::timer timerFame;
int64_t desiredTimeFame;
int64_t spendedTimeFame;
ptime oldTime;
ptime nowTime;
bool first_time;
string file_service_get_dif_path(path base_path, path new_path)
{
path sdiffpath;
path stmppath = new_path;
while(stmppath != base_path) {
sdiffpath = path(stmppath.stem().string() + stmppath.extension().string())/ sdiffpath;
stmppath = stmppath.parent_path();
}
string diff_path =sdiffpath.string();// boost::lexical_cast<string>(sdiffpath);
diff_path = diff_path.substr(0, (diff_path.length()));
std::replace(diff_path.begin(), diff_path.end(), '\\', '/');
return diff_path;
}
void is_file(path p)
{
std::string a = file_service_get_dif_path(file_service_default_path, p);
std::cout << "File: " << a << std::endl;
}
void is_new_file(path p)
{
std::time_t t = boost::filesystem::last_write_time( p );
ptime lastAccessTime = from_time_t( t );
if ( lastAccessTime >= oldTime && lastAccessTime <= nowTime )
{
std::string a = file_service_get_dif_path(file_service_default_path, p);
std::cout << "File: " << a << " is new to us." << std::endl;
}
}
void is_dir(path dir)
{
boost::filesystem::directory_iterator dirIter( dir ), dirIterEnd;
while ( dirIter != dirIterEnd )
{
if ( boost::filesystem::exists( *dirIter ) && !boost::filesystem::is_directory( *dirIter ) )
{
if (first_time)
{
is_file((*dirIter));
}
else
{
is_new_file((*dirIter));
}
}
else
{
is_dir((*dirIter));
}
++dirIter;
}
}
void files_walker()
{
while(true)
{
timerFame.restart();
oldTime = nowTime;
nowTime = second_clock::local_time() ;
file_service_default_path = file_service_default_path;
is_dir(file_service_default_path);
first_time = false;
spendedTimeFame = (int64_t)timerFame.elapsed();
cout << spendedTimeFame << std::endl;
if (spendedTimeFame < desiredTimeFame)
boost::this_thread::sleep(boost::posix_time::milliseconds(desiredTimeFame - spendedTimeFame));
}
}
int main()
{
desiredTimeFame = (int64_t)(5000.0f);
first_time = true;
file_service_default_path = "./new";
boost::thread workerThread(files_walker);
cin.get();
}
But it seems not to show any new files=( how to fix it?
Update 2:
Solved with nowTime = second_clock::universal_time();
Update 3:
fixed code:
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <boost/timer.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
using namespace boost::filesystem;
using namespace boost::posix_time;
using namespace boost::local_time;
using namespace boost::gregorian;
using namespace std;
path file_service_default_path;
boost::timer timerFame;
int64_t desiredTimeFame;
int64_t spendedTimeFame;
ptime oldTime;
ptime nowTime;
bool first_time;
string file_service_get_dif_path(path base_path, path new_path)
{
path sdiffpath;
path stmppath = new_path;
while(stmppath != base_path) {
sdiffpath = path(stmppath.stem().string() + stmppath.extension().string())/ sdiffpath;
stmppath = stmppath.parent_path();
}
string diff_path =sdiffpath.string();// boost::lexical_cast<string>(sdiffpath);
diff_path = diff_path.substr(0, (diff_path.length()));
std::replace(diff_path.begin(), diff_path.end(), '\\', '/');
return diff_path;
}
void is_file(path p)
{
std::string a = file_service_get_dif_path(file_service_default_path, p);
std::cout << "File: " << a << std::endl;
}
void is_new_file(path p)
{
std::time_t t = boost::filesystem::last_write_time( p );
ptime lastAccessTime = from_time_t( t );
if ( lastAccessTime >= oldTime && lastAccessTime <= nowTime )
{
std::string a = file_service_get_dif_path(file_service_default_path, p);
std::cout << "File: " << a << " is new to us." << std::endl;
}
}
void is_dir(path dir)
{
if(!exists(dir))
{
return;
}
boost::filesystem::directory_iterator dirIter( dir );
boost::filesystem::directory_iterator dirIterEnd;
while ( dirIter != dirIterEnd )
{
if ( boost::filesystem::exists( *dirIter ) && !boost::filesystem::is_directory( *dirIter ) )
{
if (first_time)
{
is_file((*dirIter));
}
else
{
is_new_file((*dirIter));
}
}
else
{
is_dir((*dirIter));
}
++dirIter;
}
}
void files_walker()
{
while(true)
{
timerFame.restart();
oldTime = nowTime;
nowTime = second_clock::universal_time();
is_dir(file_service_default_path);
first_time = false;
spendedTimeFame = (int64_t)timerFame.elapsed();
cout << spendedTimeFame << std::endl;
if (spendedTimeFame < desiredTimeFame)
boost::this_thread::sleep(boost::posix_time::milliseconds(desiredTimeFame - spendedTimeFame));
}
}
int main()
{
desiredTimeFame = (int64_t)(5000.0f);
first_time = true;
file_service_default_path = "./new";
boost::thread workerThread(files_walker);
cin.get();
}
As Emile Cormier stated, you will be unable to get the file creation time in Unix. However, you can still look for 'fresh' files, depending on your definition of 'fresh', by looking at last access time. This is achieved by a call as follows:
boost::filesystem::path p( "somefile" );
std::time_t t = boost::filesystem::last_write_time( p );
boost::ptime lastAccessTime = boost::ptime::from_time_t( t );
So if you then look at the boost::filesystem::directory_iterator you are able to iterate over a directory, i.e. "./" as you state in your question, and compare lastAccessTime given in the example above with the range specified in your question.
So the way I would do this, assuming oldTime has been previously defined, is as follows:
boost::filesystem::path dir( "./" );
boost::ptime nowTime( boost::second_clock::local_time() );
boost::filesystem::directory_iterator dirIter( dir ), dirIterEnd;
while ( dirIter != dirIterEnd )
{
if ( boost::filesystem::exists( *dirIter ) && !boost::filesystem::is_directory( *dirIter ) )
{
std::time_t t = boost::filesystem::last_write_time( *dirIter );
boost::ptime lastAccessTime = boost::ptime::from_time_t( t );
if ( lastAccessTime >= oldTime && lastAccessTime <= nowTime )
{
std::cout << "File meets criteria: " << (*dirIter).filename() << std::endl;
}
}
++dirIter;
}
Hope this helps!
You could refine this by persistently storing the last time you checked the directory and/or the last list of files present in the directory when you last checked, and use this as a point of comparison instead. Without more information as to your intentions I can't provide further guidance.
Relevant docs:
boost::filesystem Reference, boost::ptime Reference