How can I capture STDOUT to a string?

A handy function for capturing stdout into a string… The following method is a handy general purpose tool to capture stdout and return it as a string. (I use this frequently in unit tests where I want to verify something printed to stdout.) Note especially the use of the ensure clause to restore $stdout (and … Read more

How to send output to stderr?

After Rust 1.19 As of Rust 1.19, you can use the eprint and eprintln macros: fn main() { eprintln!(“This is going to standard error!, {}”, “awesome”); } This was originally proposed in RFC 1896. Before Rust 1.19 You can see the implementation of println! to dive into exactly how it works, but it was a … Read more

Code for printf function in C [duplicate]

Here’s the GNU version of printf… you can see it passing in stdout to vfprintf: __printf (const char *format, …) { va_list arg; int done; va_start (arg, format); done = vfprintf (stdout, format, arg); va_end (arg); return done; } See here. Here’s a link to vfprintf… all the formatting ‘magic’ happens here. The only thing … Read more

Rerouting stdin and stdout from C

Why use freopen()? The C89 specification has the answer in one of the endnotes for the section on <stdio.h>: 116. The primary use of the freopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout), as those identifiers need not be modifiable lvalues to which the value returned … Read more

GCC fatal error: stdio.h: No such file or directory

macOS I had this problem too (encountered through Macports compilers). Previous versions of Xcode would let you install command line tools through xcode/Preferences, but xcode5 doesn’t give a command line tools option in the GUI, that so I assumed it was automatically included now. Try running this command: xcode-select –install If you see an error … Read more

stdlib and colored output in C

All modern terminal emulators use ANSI escape codes to show colours and other things. Don’t bother with libraries, the code is really simple. More info is here. Example in C: #include <stdio.h> #define ANSI_COLOR_RED “\x1b[31m” #define ANSI_COLOR_GREEN “\x1b[32m” #define ANSI_COLOR_YELLOW “\x1b[33m” #define ANSI_COLOR_BLUE “\x1b[34m” #define ANSI_COLOR_MAGENTA “\x1b[35m” #define ANSI_COLOR_CYAN “\x1b[36m” #define ANSI_COLOR_RESET “\x1b[0m” int main … Read more

‘printf’ vs. ‘cout’ in C++

I’m surprised that everyone in this question claims that std::cout is way better than printf, even if the question just asked for differences. Now, there is a difference – std::cout is C++, and printf is C (however, you can use it in C++, just like almost anything else from C). Now, I’ll be honest here; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)