How can you flush a write using a file descriptor?
I think what you are looking for may be int fsync(int fd); or int fdatasync(int fd); fsync will flush the file from kernel buffer to the disk. fdatasync will also do except for the meta data.
I think what you are looking for may be int fsync(int fd); or int fdatasync(int fd); fsync will flush the file from kernel buffer to the disk. fdatasync will also do except for the meta data.
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
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
I got same problem with a project porting from Visual Studio 2013 to Visual Studio 2017. Fix: change Properties → General → Windows SDK Version to 10
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
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
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
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
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