file_system


The class os_file_system is comprised of static functions that perform a variety of actions on a file system entry. A file system entry usually is either a file or a directory, but can be a link, a named pipe, a device, or a socket on UNIX systems.

The os_file_system class checks for a file's existence and can remove, rename, and resize a file using a portable interface. Other platform-specific operations may be available; consult PART 2, "Reference Manual," for details.

The following example creates a dummy file and illustrates some of the common os_file_system functions.

Example <ospace/file/examples/filesys1.cpp>
#include <iostream>
#include <ospace/file.h>

void
main()
  {
  os_file_toolkit initialize;

  const char* old_name = "filesys1.old";
  os_file file( old_name, os_open_control::create_always );
  file.close();
  cout << old_name << " exists = ";
  cout << os_file_system::exists( old_name ) << endl;

  const char* new_name = "filesys1.new";
  cout << "Renaming file from " << old_name;
  cout << " to " << new_name << endl;
  os_file_system::rename( old_name, new_name );
  cout << old_name << " exists = ";
  cout << os_file_system::exists( old_name ) << endl;
  cout << new_name << " exists = ";
  cout << os_file_system::exists( new_name ) << endl;

  cout << "Removing " << new_name << endl;
  os_file_system::remove( new_name );
  cout << new_name << " exists = ";
  cout << os_file_system::exists( new_name ) << endl;

#if defined OS_WIN32
  // utf-8 
  // create a file ?-?, \u2f7a-\u2f81. \xe2\xbd\xba-\xe2\xbe\x81 in utf-8
  try
  {
  const char* old_name_u8 = "\xe2\xbd\xba-\xe2\xbe\x81";
  os_file file_u8( old_name_u8, os_open_control::create_always, os_io_control::read_write_access, 0, Utf8 );
  file_u8.close();
  cout <<  "old u8 file exists = ";
  cout << os_file_system::exists( old_name_u8, Utf8 ) << endl;

  // create a new file ?-? \2fc5-\u2f81. \xe2\xbf\x85-\xe2\xbe\x81
  const char* new_name_u8 = "\xe2\xbf\x85-\xe2\xbe\x81";
  cout << "Renaming utf-8 file " << endl;

  os_file_system::rename( old_name_u8, new_name_u8, Utf8 );

  cout << " old u8 file exists = ";
  cout << os_file_system::exists( old_name_u8, Utf8 ) << endl;

  cout <<  " new u8 file exists = ";
  cout << os_file_system::exists( new_name_u8, Utf8 ) << endl;

  cout << "Removing new u8 file" << endl;
  os_file_system::remove( new_name_u8,  Utf8 );
  cout <<  " new u8 file exists = ";
  cout << os_file_system::exists( new_name_u8, Utf8 ) << endl;
  }
  catch( os_file_toolkit_error& error )
  {
  cout << "Caught exception: " << endl;
  cout << error.what() << endl;
  cout << error.native() << endl;
  cout << error.code() << endl;
  }
#endif	

  }

filesys1.old exists = 1
Renaming file from filesys1.old to filesys1.new
filesys1.old exists = 0
filesys1.new exists = 1
Removing filesys1.new
filesys1.new exists = 0
old u8 file exists = 1
Renaming utf-8 file
old u8 file exists = 0
new u8 file exists = 1
Removing new u8 file
new u8 file exists = 0



Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.