(C++) std::putc
January 11, 2018 · View on GitHub
(C++) std::putc
std::putc is a C-style function to append a character to a file.
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 12.04 (precise)
Qt Creator 2.4.1
- G++ 4.6.3
Libraries used:
Qt project file: Cppstd::putc.pro
QT += core QT -= gui TARGET = Cppstd::putc CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp
main.cpp
#include <cassert> #include <cstdio> #include <string> int main () { const std::string filename = "tmp.txt"; const char my_char = 'x'; { //Open file for writing, clears the file when opening FILE * const file = std::fopen(filename.c_str(),"w"); //Write my_char to file std::putc(my_char,file); //Close the file std::fclose (file); } { //Open file for reading FILE * const file = std::fopen(filename.c_str(),"r"); //Read char from file const char c = std::getc(file); //Close the file std::fclose (file); //Assume the character written is the same as the one read assert(my_char == c); } }