Just a thread for people to ask and answer C++ related questions.
[As I'm stuck learning it (to benefit the community of course).]
[As I'm stuck learning it (to benefit the community of course).]
#include <stdio.h>
void console(const char *_sFormat, ...);
int main () {
console("Hello World!");
return 0;
}
void console(const char *_sFormat, ...) {
printf("[APP] %s\n", _sFormat);
}
#include <stdio.h>
#include <stdarg.h>
/**
* Prints to stdout as console.
*
* @param _sFormat The string and the format of the string to print & save.
* @parma ... Any Arguments.
* @return VOID
*/
void console(const char *_sFormat, ...) {
char sDest[256]; // In a console the max useful size is really 72.
// list of arguments.
va_list vaArgs;
// parse the list from the ellipsis ('...').
va_start(vaArgs, _sFormat);
// Save the formatted string into _sDest.
vsprintf(sDest, _sFormat, vaArgs);
// closes the argument list.
va_end(vaArgs);
printf("[APP] %s\n", sDest);
}
void ConsoleFormat(const char *szFormat, ...)
{
static char szBuffer[1024];
va_list va;
va_start(va, szFormat);
vsprintf_s(szBuffer, szFormat, va);
va_end(va);
cout << [API] << szBuffer << '\n';
}
char *pBigBuffer = new char[1024*1024*100]; //100mb buffer should be big enough for anything. Worry about running out of memory/slow allocation...
ConsoleFormat(pBigBuffer, "%s%s%s", codeDump, dataDump, logDump");
delete [] pBigBuffer;
#include <iostream>
#include <string>
using namespace std;
class Logger {
public:
Logger(ostream& os, string prefix = "[APP] ") : mOut(os), mPrefix(prefix) {}
ostream& log() {
return mOut << mPrefix;
}
private:
ostream& mOut;
string mPrefix;
};
int main() {
Logger l(cout);
l.log() << "Test" << endl;
l.log() << "Line 2";
return 0;
}
std::string strHello = "Hello";
char szHello[] = "Hello";
printf("char * = %s\n", szHello);
printf("string = %s\n", strHello); //BAD CODE
printf("string = %s\n", strHello.c_str()); //Good.
//OR the equivalent
cout << "char * = " << szHello << '\n';
cout << "string = " << strHello << '\n'; //works fine because string overloaded the << with iostream.
cout << "string = " << strHello.c_str() << '\n'; //also works
vsnprintf(sDest, sizeof(sDest) - 1, _sFormat, vaArgs);
void console(const char *_sFormat, ...) {
// Varable Defines
char* sArg;
int iSize = 0;
// list of arguments.
va_list(vaArgs);
// parse the list from the ellipsis ('...').
va_start(vaArgs, _sFormat);
// Length of Formatted String + NUL terminator
iSize = sizeof(_sFormat) + 1;
// While we still have more args ...
do {
// Get argument in place in char array (string).
sArg = va_arg(vaArgs, char*);
// Add the size of the string to iSize for a running total.
iSize += sizeof(sArg);
} while (sArg != NULL);
// Define our Desintation string.
char sDest[iSize];
// And write the information to the destination.
vsnprintf(sDest, sizeof(sDest) - 1, _sFormat, vaArgs);
// closes the argument list.
va_end(vaArgs);
printf("[ISM] %s\n", sDest);
}
char sDest[666]; // this is put on the stack, the compiler knows the size at compile time.
char* psDest;
psDest = new char[666]; // this is put on to the heap so you have to free it afterwards
delete [] psDest;
psDest = 0; // good programming practise to set the pointer to zero