Coses útils C++
Salta a la navegació
Salta a la cerca
Contingut
Redireccionar sortida
A vegades interessa distingir entre la sortida estàndar, la sortida d'errors o els log:
// http://cmasomenos.blogspot.com/2010/09/iostream-redireccionar-clog-para.html // g++ -o prova prova.cpp // ./prova 1>stdout.txt 2>stderr.txt // $ cat stderr.txt // B // $ cat stdout.txt // A // $ cat test.log // C // o bé més senzill: // ./prova > hola.txt #include <iostream> #include <fstream> using namespace std; int main() { // Creamos un archivo de salida para logging. ofstream test_log; test_log.open("test.log"); // Obtenemos el streambuf actual de clog (esto // lo usaremos luego para restaurar el streambuf // a su valor original, por si las moscas). streambuf* old_rdbuf = clog.rdbuf(); // Reemplazamos el streambuf de clog con el del archivo. // Ahora ambos streams utilizarán el mismo streambuf (es // decir, escriben en el archivo test.log). clog.rdbuf(test_log.rdbuf()); // Hacemos lo mismo que el ejemplo original. cout << "A\n"; printf("hola joan\n"); cerr << "B\n"; clog << "C\n"; // Restauramos el viejo streambuf de clog. clog.rdbuf(old_rdbuf); // Cerramos el archivo. test_log.close(); return 0; }
printf (sortida) amb colors, negreta i subratllat
$ gcc -o cprintf_test cprintf_test.c
cprintf.h:
//////////////////////////////////////////////////////////////////////////////////////////////////// // // (C)2011 Edwards Research Group // You are licensed to use this work under a CC-BY-SA License. // See: http://blog.edwards-research.com/about/ // http://creativecommons.org/licenses/by-sa/3.0/us/ // //////////////////////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <stdarg.h> #define CLR_BLACK 0 #define CLR_RED 1 #define CLR_GREEN 2 #define CLR_YELLOW 3 #define CLR_BLUE 4 #define CLR_MAGENTA 5 #define CLR_CYAN 6 #define CLR_WHITE 7 #define ATTR_NONE 0 #define ATTR_BOLD 1 #define ATTR_DIM 2 #define ATTR_UNDERLINE 4 #define ATTR_BLINK 5 #define ATTR_REVERSE 7 void cprint_init(int fg, int attr, int bg) { if(bg != -1){ printf("%c[%d;%d;%dm",27,attr,(30+fg),(40+bg)); } else{ printf("%c[%d;%dm",27,attr,(30+fg)); } } void cprint_rst(void) { printf("%c[%dm", 27, 0); } int cprintf(int fg, int a, int bg, char * fmt, ...) { va_list args; va_start(args, fmt); cprint_init(fg,a,bg); vprintf(fmt, args); cprint_rst(); va_end(args); }
cprintf_test.c:
//////////////////////////////////////////////////////////////////////////////////////////////////// // // (C)2011 Edwards Research Group // You are licensed to use this work under a CC-BY-SA License. // See: http://blog.edwards-research.com/about/ // http://creativecommons.org/licenses/by-sa/3.0/us/ // //////////////////////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include "cprintf.h" void main(void) { printf("Unformatted...\n"); // Example with cprintf(4,1,-1,"Blue Bold...\n"); cprintf(CLR_GREEN,ATTR_UNDERLINE,-1,"Green Underlined...\n"); cprintf(CLR_YELLOW,ATTR_NONE,CLR_RED,"Yellow on Red..."); printf("\n\n"); int i,j; for(i=0; i<8; i++) { for(j=0; j<8; j++) { if(j == 3 | j == 6){ continue; } cprintf(i,j,-1,"FG=%d,A=%d", i, j); printf(" "); } printf("\n"); } printf("\n"); return; }
I el codi mínim per fer negreta i subratllat, sense include: negreta.cpp
$ g++ -o negreta negreta.cpp
#include <stdio.h> #include <stdarg.h> #define ATTR_BOLD 1 #define ATTR_UNDERLINE 4 void cprint_init(int fg, int attr, int bg) { if(bg != -1){ printf("%c[%d;%d;%dm",27,attr,(30+fg),(40+bg)); } else{ printf("%c[%d;%dm",27,attr,(30+fg)); } } void cprint_rst(void) { printf("%c[%dm", 27, 0); } int cprintf(int fg, int a, int bg, char * fmt, ...) { va_list args; va_start(args, fmt); cprint_init(fg,a,bg); vprintf(fmt, args); cprint_rst(); va_end(args); } int main(void) { printf("Unformatted...\n"); cprintf(-1,1,-1,"negreta\n"); cprintf(-1,4,-1,"subratllat\n"); printf("\n"); return 0; }
char * amb caràcters estranys
Recordem que hi ha el caràcter NULL terminating character: '\0'. M'hi he barallat bastant en el projecte smf_parser.
creat per Joan Quintana Compte, gener 2012