Examples › General Examples
general_disk_file_class
C++17 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_general/disk_file_class/testprog.cpp · Modern: examples_modern/general/disk_file_class/modern.cpp
Modern equivalent of the DiskFileB test program. std::fstream reads and writes files, text and binary: open/create (df_Open, df_Create), read, write, seekg/seekp and tellg (df_Seek, df_Tell), flush, close. The file system calls of DiskFileB (df_Exists, df_IsFile, df_IsDirectory, df_FileSize, df_copy, df_rename, df_remove, df_mkdir, df_rmdir, df_chdir, df_pwd) are std::filesystem functions. std::setw, setfill and setprecision replace df_SetWidth, df_SetFill and df_Precision.
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| SetWidth(), SetFill(), Precision(), sprintf() | std::format("{:*>10} {:.3f}", ...) | C++20 | C++17 and older: <iomanip> setw, setfill, setprecision. |
| new / delete | automatic objects, containers, std::unique_ptr / std::make_unique | C++11 / C++14 | Ownership is in the type, and release() shows a hand-over. |
| strcpy(), strncpy(), memmove() into char arrays | std::string; std::array<char, N> + string_view::copy() | C++11 / C++17 | |
| DiskFileB | std::fstream (read, write, seekg/seekp, tellg) | C++98 | |
| futils_*(), df_mkdir(), df_copy() ... | std::filesystem (directory_iterator, create_directory, file_size, copy_file, rename, remove, current_path) | C++17 | Errors through std::error_code or std::filesystem_error. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_general/disk_file_class/testprog.cpp 514 lines
#include "gxdlcode.h" #if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library #include <iostream> #include <iomanip> using namespace std; // Use unqualified names for Standard C++ library #else // Use the old iostream library by default #include <iostream.h> #include <iomanip.h> #endif // __USE_ANSI_CPP__ #include <string.h> #include <stdio.h> #include "dfileb.h" #ifdef __MSVC_DEBUG__ #include "leaktest.h" #endif void PausePrg() { cout << "\n"; cout << "Press enter to continue..." << "\n"; cin.get(); } void DirectoryTest() { cout << "Testing general purpose directory functions" << "\n"; const char *dirname = "testdir"; // Directory to be created // Directory immediately above the current directory position const char *parentdir = ".."; DiskFileB dir; cout << "Creating " << dirname << " directory" <<"\n"; if(dir.df_mkdir(dirname) != 0) { cout << dir.df_ExceptionMessage() << "\n"; return; } if((dir.df_Exists(dirname)) && (dir.df_IsDirectory(dirname))) { cout << "The " << dirname << " directory was created" << "\n"; } cout << "Changing directory to " << dirname << " directory" <<"\n"; if(dir.df_chdir(dirname) != 0) { cout << dir.df_ExceptionMessage() << "\n"; return; } char *pwd[df_MAX_DIR_LENGTH]; char drive_letter; if(dir.df_pwd((char *)pwd) == 0) { cout << "Present working directory: " << (char *)pwd << "\n"; if(dir.df_HasDriveLetter((const char *)pwd, drive_letter)) { cout << "DOS Drive letter = " << drive_letter << "\n"; } } cout << "Changing directory to " << parentdir << " directory" <<"\n"; if(dir.df_chdir(parentdir) != 0) { cout << dir.df_ExceptionMessage() << "\n"; return; } if(dir.df_pwd((char *)pwd) == 0) { cout << "Present working directory: " << (char *)pwd << "\n"; if(dir.df_HasDriveLetter((const char *)pwd, drive_letter)) { cout << "DOS Drive letter = " << drive_letter << "\n"; } } cout << "Removing " << dirname << " directory" <<"\n"; if(dir.df_rmdir(dirname) != 0) { cout << dir.df_ExceptionMessage() << "\n"; return; } } void FileTest() { cout << "Testing general purpose file functions" << "\n"; const char *testfile1 = "testfile1.dat"; const char *testfile2 = "testfile2.dat"; const char *testfile3 = "testfile3.dat"; DiskFileB dfile(testfile1, DiskFileB::df_READWRITE, DiskFileB::df_CREATE); if(!dfile) { cout << "Could not open the " << testfile1 << "\n"; return; } if((dfile.df_Exists(testfile1)) && (dfile.df_IsFile(testfile1))) { cout << "The " << testfile1 << " file was opened with read/write access" << "\n"; } const char *teststr1 = "The quick brown fox jumps over the lazy dog \ 0123456789\r\n"; cout << "Writing " << strlen(teststr1) << " bytes to " << testfile1 << "\n"; if(dfile.df_Write((const char *)teststr1, strlen(teststr1)) != 0) { cout << dfile.df_ExceptionMessage() << "\n"; return; } cout << testfile1 << " Dynamic file size = " << (int)dfile.df_GetEOF() << "\n"; dfile.df_Flush(); cout << testfile1 << " Static file size = " << (int)dfile.df_FileSize(testfile1) << "\n"; char *sbuf = new char[strlen(teststr1)+1]; cout << "Reading the file contents..." << "\n"; if(dfile.df_Read((char *)sbuf, strlen(teststr1), (FAU_t)0) != 0) { cout << dfile.df_ExceptionMessage() << "\n"; delete[] sbuf; return; } sbuf[strlen(teststr1)] = '\0'; cout << sbuf; delete[] sbuf; dfile.df_Close(); cout << "Renaming the " << testfile1 << " to " << testfile2 << "\n"; if(dfile.df_rename(testfile1, testfile2) != 0) { cout << dfile.df_ExceptionMessage() << "\n"; return; } cout << "Copying " << testfile2 << " to " << testfile3 << "\n"; if(dfile.df_copy(testfile2, testfile3) != 0) { cout << dfile.df_ExceptionMessage() << "\n"; return; } cout << "Removing " << testfile2 << " and " << testfile3 << "\n"; if(dfile.df_remove(testfile2) != 0) { cout << dfile.df_ExceptionMessage() << "\n"; return; } if(dfile.df_remove(testfile3) != 0) { cout << dfile.df_ExceptionMessage() << "\n"; return; } // Add routine to change file attributes/permissions here // ------------------------------------------------------ } void RandomSeekTest() { struct fileblock { unsigned block_length; }; cout << "Performing random read/write test" << "\n"; const char *testfile = "testfile.dat"; DiskFileB dfile(testfile, DiskFileB::df_READWRITE, DiskFileB::df_CREATE); if(!dfile) { cout << "Could not open the " << testfile << "\n"; return; } if((dfile.df_Exists(testfile)) && (dfile.df_IsFile(testfile))) { cout << "The " << testfile << " file was opened with read/write access" << "\n"; } const char *teststr = "The quick brown fox jumps over the lazy dog"; unsigned i; unsigned len = strlen(teststr)+4; char *str = new char[len+1]; for(i = 0; i< len; i++) str[i] = '\0'; memmove(str, teststr, strlen(teststr)); const unsigned blocks_to_write = 10; FAU_t pos[blocks_to_write]; fileblock hdr; char sbuf[df_MAX_LINE_LENGTH]; cout << "Writing " << blocks_to_write << " blocks of data..." << "\n"; for(i = 0; i < blocks_to_write; i++) { pos[i] = dfile.df_Tell(); sprintf(sbuf, " %u", i); memmove(str+strlen(teststr), sbuf, strlen(sbuf)); str[strlen(teststr)+strlen(sbuf)] = '\0'; hdr.block_length = sizeof(fileblock) + (strlen(str)-1); dfile.df_Write(&hdr,sizeof(fileblock)); dfile.df_Write((char *)str, hdr.block_length); } cout << "Reading back the blocks..." << "\n"; PausePrg(); const int max_lines = 10; int lines_printed = 0; hdr.block_length = 0; int ii; for(i = 0; i < blocks_to_write; i++) { for(ii = 0; ii < (int)len; ii++) str[ii] = '\0'; dfile.df_Read(&hdr, sizeof(fileblock), pos[i]); dfile.df_Read((char *)str, hdr.block_length, (pos[i]+sizeof(fileblock))); cout << "Block address = " << (long)pos[i] << "\n"; cout << "Block size = " << hdr.block_length << "\n"; cout << "Data = " << str << "\n"; lines_printed++; if(lines_printed >= max_lines) { PausePrg(); lines_printed = 0; } } dfile.df_Rewind(); cout << "Reading back the blocks in reverse order..." << "\n"; PausePrg(); for(ii = (int)blocks_to_write-1; ii >= 0; ii--) { for(i = 0; i < len; i++) str[i] = '\0'; dfile.df_Read((char *)&hdr, sizeof(fileblock), pos[ii]); dfile.df_Read((char *)str, hdr.block_length); cout << "Block address = " << (long)pos[ii] << "\n"; cout << "Block size = " << hdr.block_length << "\n"; cout << "Data = " << str << "\n"; lines_printed++; if(lines_printed >= max_lines) { PausePrg(); lines_printed = 0; } } delete[] str; dfile.df_Close(); cout << "Removing " << testfile << "\n"; if(dfile.df_remove(testfile) != 0) { cout << dfile.df_ExceptionMessage() << "\n"; return; } return; } void GetLineTest() { cout << "Testing the read line function" << "\n"; const char *DOSTextLine = "The quick brown fox jumps over the lazy dog\r\n"; const char *UNIXTextLine = "The quick brown fox jumps over the lazy dog\n"; cout << "Creating DOS and UNIX text files" << "\n"; const char *fname1 = "dostext.txt"; const char *fname2 = "unixtext.txt"; DiskFileB f1(fname1, DiskFileB::df_READWRITE, DiskFileB::df_CREATE); if(!f1) { cout << "Error opening text file" << "\n"; return; } DiskFileB f2(fname2, DiskFileB::df_READWRITE, DiskFileB::df_CREATE); if(!f2) { cout << "Error opening text file" << "\n"; return; } const int num_lines = 10; int i; cout << "Writing " << num_lines << " lines to the text files" << "\n"; for(i = 0; i < num_lines; i++) { f1.df_Write(DOSTextLine, strlen(DOSTextLine)); f2.df_Write(UNIXTextLine, strlen(UNIXTextLine)); if((f1.df_CheckError()) || (f2.df_CheckError())) { cout << "Error writing to text file" << "\n"; return; } } cout << "Reading the DOS text file" << "\n"; PausePrg(); char sbuf[255]; int line_number = 1; f1.df_Rewind(); // Reset the file pointer to the beginning of the stream while(!f1.df_EOF()) { f1.df_GetLine(sbuf, sizeof(sbuf)); if(f1.df_GetError() != DiskFileB::df_NO_ERROR) { cout << "Error reading from the text file" << "\n"; cout << f1.df_ExceptionMessage() << "\n"; break; } cout << line_number++ << ' ' << sbuf << "\n"; } cout << "\n"; cout << "Reading the UNIX text file" << "\n"; PausePrg(); line_number = 1; f2.df_Rewind(); // Reset the file pointer to the beginning of the stream while(!f2.df_EOF()) { // Loop until EOF error f2.df_GetLine(sbuf, sizeof(sbuf)); if(f2.df_GetError() != DiskFileB::df_NO_ERROR) { cout << "Error reading from the text file" << "\n"; cout << f2.df_ExceptionMessage() << "\n"; break; } cout << line_number++ << ' ' << sbuf << "\n"; } // Close and remove the text files f1.df_Close(); f2.df_Close(); f1.df_remove(fname1); f2.df_remove(fname2); } void OverLoadTest() { cout << "Testing overloaded << and >> operators" << "\n"; cout << "\n"; const char *fname = "strtest.dat"; DiskFileB stream(fname, DiskFileB::df_READWRITE, DiskFileB::df_CREATE); if(!stream) { cout << "Error opening text file" << "\n"; cout << stream.df_ExceptionMessage() << "\n"; return; } const char *s1 = "The quick brown fox jumps over the lazy dog"; char *s2 = "0123456789"; char c1 = 'A'; const char c2 = 'B'; cout << "Writing some text to the stream" << "\n"; stream << s1 << ' ' << s2 << "\n"; stream << c1 << ' ' << c2 << "\n"; cout << "Reading back the text from the stream" << "\n"; stream.df_Rewind(); char sbuf1[df_MAX_LINE_LENGTH]; char sbuf2[df_MAX_LINE_LENGTH]; char ch1, ch2, ch3; stream >> sbuf1; stream >> ch1; stream >> ch2; stream >> ch3; // Echo the text to the console cout << sbuf1; cout << ch1 << ch2 << ch3 << "\n"; cout << "Writing some integer values to the stream in binary" << "\n"; FAU_t stream_pos = stream.df_Tell(); // Record the current position const short i1 = 1; short i2 = 2; const unsigned short i3 = 3; unsigned short i4 = 4; const int i5 = 5; int i6 = 6; const unsigned int i7 = 7; unsigned int i8 = 8; const long i9 = 9; long i10 = 10; const unsigned long i11 = 11; unsigned long i12 = 12; stream << i1 << i2 << i3 << i4 << i5 << i6 << i7 << i8 << i9 << i10 << i11 << i12; cout << "Reading the binary integer values from the stream" << "\n"; stream.df_Seek(stream_pos); // Reset the stream position to the first value short in1, in2; unsigned short in3, in4; int in5, in6; unsigned int in7, in8; long in9, in10; unsigned long in11, in12; stream >> in1 >> in2 >> in3 >> in4 >> in5 >> in6 >> in7 >> in8 >> in9 >> in10 >> in11 >> in12; // Echo the values to the console cout << in1 << " " << in2 << " " << in3 << " " << in4 << " " << in5 << " " << in6 << " " << in7 << " " << in8 << " " << in9 << " " << in10 << " " << in11 << " " << in12 << "\n"; cout << "Writing some integer values to the stream in text mode" << "\n"; stream_pos = stream.df_Tell(); // Record the current position // Use decimal and hex text modes stream << text << i1 << " " << i2 << " " << i3 << " " << i4 << " " << i5 << " " << i6 << " " << i7 << " " << i8 << " " << i9 << " " << i10 << " " << i11 << " " << i12 << "\n" << flush; stream.df_SetWidth(2); stream.df_SetFill('0'); stream << hex << i1 << " " << i2 << " " << i3 << " " << i4 << " " << i5 << " " << i6 << " " << i7 << " " << i8 << " " << i9 << " " << i10 << " " << i11 << " " << i12 << "\n" << clear; cout << "Reading the text strings from the stream" << "\n"; stream.df_Seek(stream_pos); // Reset the stream position to the first value stream >> sbuf1 >> sbuf2; // Echo the text to the console cout << sbuf1 << "\n" << sbuf2 << "\n"; cout << "Writing some floating point values to the stream in binary" << "\n"; stream_pos = stream.df_Tell(); // Record the current position float f1 = 1.1011; const float f2 = 2.2022; double f3 = 3.3033; const double f4 = 4.4044; stream << f1 << f2 << f3 << f4; cout << "Reading the binary floating point values from the stream" << "\n"; stream.df_Seek(stream_pos); // Reset the stream position to the first value float fn1, fn2; double fn3, fn4; stream >> fn1 >> fn2 >> fn3 >> fn4; // Echo the values to the console cout << fn1 << " " << fn2 << " " << fn3 << " " << fn4 << "\n"; cout << "Writing some floating point values to the stream in text mode" << "\n"; stream_pos = stream.df_Tell(); // Record the current position stream << text << f1 << " " << f2 << " " << f3 << " " << f4 << "\n"; stream.df_Precision(4); stream << f1 << " " << f2 << " " << f3 << " " << f4 << "\n"<< clear; cout << "Reading the text strings from the stream" << "\n"; stream.df_Seek(stream_pos); // Reset the stream position to the first value stream >> sbuf1 >> sbuf2; // Echo the text to the console cout << sbuf1 << "\n" << sbuf2 << "\n"; #if defined (__64_BIT_DATABASE_ENGINE__) PausePrg(); cout << "Writing some 64-bit integer values to the stream in binary" << "\n"; stream_pos = stream.df_Tell(); // Record the current position __LLWORD__ LL1 = (__LLWORD__)1; const __LLWORD__ LL2 = (__LLWORD__)2; __ULLWORD__ LL3 = (__ULLWORD__)3; const __ULLWORD__ LL4 = (__LLWORD__)4; stream << LL1 << LL2 << LL3 << LL4; cout << "Reading the binary 64-bit integer point values from the stream" << "\n"; stream.df_Seek(stream_pos); // Reset the stream position to the first value __LLWORD__ LLn1, LLn2; __ULLWORD__ LLn3, LLn4; stream >> LLn1 >> LLn2 >> LLn3 >> LLn4; // Echo the values to the console cout << (int)LLn1 << " " << (int)LLn2 << " " << (int)LLn3 << " " << (int) LLn4 << "\n"; cout << "Writing some 64-bit integer values to the stream in text mode" << "\n"; stream_pos = stream.df_Tell(); // Record the current position // Use decimal and hex text modes LL1 = __LLWORD__(9223372036854775808); LL3 = __ULLWORD__(18446744073709551615); stream << text << LL1 << " " << LL2 << " " << LL3 << " " << LL4 << "\n"; stream.df_SetWidth(4); stream.df_SetFill('0'); stream << hex << LL1 << " " << LL2 << " " << LL3 << " " << LL4 << "\n" << clear; cout << "Reading the text strings from the stream" << "\n"; stream.df_Seek(stream_pos); // Reset the stream position to the first value stream >> sbuf1 >> sbuf2; // Echo the text to the console cout << sbuf1 << "\n" << sbuf2 << "\n"; #endif // __64_BIT_DATABASE_ENGINE__ // Close and remove the text files stream.df_Close(); stream.df_remove(fname); } int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif cout << "\n"; OverLoadTest(); PausePrg(); DirectoryTest(); PausePrg(); FileTest(); PausePrg(); RandomSeekTest(); PausePrg(); GetLineTest(); cout << "\n"; cout << "Exiting..." << "\n"; cout << "\n"; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++17)
examples_modern/general/disk_file_class/modern.cpp 89 lines
#include <cstdint> #include <cstdio> #include <filesystem> #include <fstream> #include <iomanip> #include <iostream> #include <string> namespace fs = std::filesystem; struct Record { // A fixed size binary record std::int32_t id; double value; char name[16]; }; int main() { const fs::path fname = "testfile.dat"; const fs::path tname = "testfile.txt"; std::cout << "Writing 10 binary records to " << fname << "\n"; { std::ofstream out(fname, std::ios::binary | std::ios::trunc); // df_Create if(!out) { std::cout << "Cannot create " << fname << "\n"; return 1; } for(std::int32_t i = 0; i < 10; i++) { Record r{i, i * 1.5, {}}; std::snprintf(r.name, sizeof(r.name), "record %d", int(i)); out.write(reinterpret_cast<const char *>(&r), sizeof r); // df_Write } } // ~ofstream() flushes and closes std::cout << "File size: " << fs::file_size(fname) << " bytes\n"; std::cout << "Reading record 7 with seekg (df_Seek)\n"; std::fstream io(fname, std::ios::in | std::ios::out | std::ios::binary); io.seekg(7 * std::streamoff(sizeof(Record))); Record r{}; if(io.read(reinterpret_cast<char *>(&r), sizeof r)) std::cout << "id " << r.id << ", value " << r.value << ", name " << r.name << ", tellg " << io.tellg() << "\n"; std::cout << "Changing record 7 in place (df_READWRITE)\n"; r.value = 99.5; io.seekp(7 * std::streamoff(sizeof(Record))); io.write(reinterpret_cast<const char *>(&r), sizeof r); io.flush(); io.seekg(7 * std::streamoff(sizeof(Record))); io.read(reinterpret_cast<char *>(&r), sizeof r); std::cout << "value is now " << r.value << "\n"; std::cout << "Reading past the end sets eof: "; io.seekg(0, std::ios::end); io.read(reinterpret_cast<char *>(&r), sizeof r); std::cout << io.eof() << ", read " << io.gcount() << " bytes\n"; io.close(); std::cout << "\nText output with width, fill and precision\n"; { std::ofstream txt(tname); txt << std::setw(10) << std::setfill('*') << 42 << "\n" << std::setprecision(10) << 3.14159265358979 << "\n" << std::fixed << std::setprecision(2) << 1e300 << "\n"; } std::ifstream txt(tname); for(std::string line; std::getline(txt, line);) // df_GetLine() std::cout << line.substr(0, 40) << (line.size() > 40 ? "..." : "") << "\n"; txt.close(); std::cout << "\nFile system functions\n"; std::cout << "exists: " << fs::exists(fname) << ", regular file: " << fs::is_regular_file(fname) << ", directory: " << fs::is_directory(fname) << "\n"; fs::copy_file(fname, "testfile.copy", fs::copy_options::overwrite_existing); fs::rename("testfile.copy", "testfile.renamed"); std::cout << "copied and renamed: " << fs::exists("testfile.renamed") << "\n"; fs::create_directory("testdir"); std::cout << "mkdir: " << fs::is_directory("testdir") << "\n"; std::cout << "pwd: " << fs::current_path().string() << "\n"; fs::remove("testdir"); fs::remove("testfile.renamed"); fs::remove(fname); fs::remove(tname); std::cout << "removed: " << !fs::exists(fname) << "\n"; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17 cmake --build build-modern --target general_disk_file_class ./build-modern/general_disk_file_class # or without CMake: g++ -std=c++17 -pthread -o general_disk_file_class examples_modern/general/disk_file_class/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Writing 10 binary records to "testfile.dat" File size: 320 bytes Reading record 7 with seekg (df_Seek) id 7, value 10.5, name record 7, tellg 256 Changing record 7 in place (df_READWRITE) value is now 99.5 Reading past the end sets eof: 1, read 0 bytes Text output with width, fill and precision ********42 3.141592654 1000000000000000052504760255204420248704... File system functions exists: 1, regular file: 1, directory: 0 copied and renamed: 1 mkdir: 1 pwd: build-modern/run removed: 1