Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::putc

 

std::putc is a C-style function to append a character to a file.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

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);
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml