C++ Generic file writer class














































C++ Generic file writer class




Description:

Generic file writer class which will be used for file writing while debugging any code in the live scenarios. 

Motivation:

While debugging in the live network, where we cannot apply gdb and also variables are not capturing in the log file, then we are creating some temporary file and capturing debugging info into it.

Advantages:
  1. Easy to integrate this FileWriter class into your code for debugging.
  2. A generic write() function has been exposed which will use to write any type of the variable of the basic datatype into the file.
  3. This write function will work with the class object as well, However, in this case, you need to specialize the template based write() function.
  4. This may be used with those cases as well, where gdb is not available for debugging.

Example:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class FileWriter {

   private:
      ofstream mFile;

   public:
      FileWriter(std::string fileName) {
         mFile.open(fileName.c_str());
      }   

      template <typename T>
      void write(T data) {
         mFile << data << endl;
      }   
    
      ~FileWriter() {
         mFile.close();
      }   
};

int main() {
   FileWriter fw("test.txt");
   fw.write<int> (123);
   std::string str = "cppsecrets.com";
   fw.write<std::string> (str);
   return 0;
}



More Articles of Pankaj Choudhary:

Name Views Likes
Coding Challenges: Fun Projects for Student Programmers 206 0
5 Must-Have Programming Skills for Student 150 0
Hands-On Experience: Internships and Capstone Projects in Data Science Programs 423 0
5 Tips for Effective Self-Study in Programming Education 240 0
5 Essential Study Techniques for Mastering C++ 370 0
What You Need to Know about Programming Languages 217 0
Pitfalls of Learning Programming 245 0
Truminds 5G UPF supports MPTCP Proxy 10079 214
C++ Assignment Help: Where Can You Find? 574 0
Top Benefits of Learning Coding in College or University 502 0
C++ Abstract Class 938 21
Compile C++11 program using g++ 4626 10
C++ Searching an element in a vector 845 11
Priority Queue 1932 12
Check String is palindrome or not 1339 10
Preorder Tree Traversal with recursion 1240 12
Template based linked list 6078 11
Print all processes running as root 935 28
C++ Queue of objects 5326 18
How to cin to vector 19359 10
Python List Built-In Methods 679 11
Python Basic Programs 646 1
C++ Compare version string 3391 12
C++ Vector of objects 6702 20
Windows enable NTP Server 961 17
Tokenized securities on blockchain 829 0
Difference between remove and remove_if in STL 1215 19
CPU usage per thread without top utility 3274 16
Python Selected Interns (Feb 2019 Batch - 1) 1097 24
Complex Number Program 1178 10
Functor with arguments 1407 10
Pass function pointer to a function 590 11
C++ Two dimensional vector 996 21
C++ challange for Aug 2019 Interns 6169 4
C program to get memory usage 10406 18
Python challange for Interns (Feb 2019, Batch 2) 939 13
C++ Read file line by line 973 14
Dynamic Programming - Egg Dropping Problem 1856 16
GDB Print complete string 3477 24
UDP Server Program in C 9872 24
C++ Split string by delimiter and store it in vector 3402 12
Python programs list 838 23
Mutable lambda in C++ 5624 11
C++ Sorting of vector 691 10
Overload Assignment Operator 635 10
Blockchain The Benefits and Risks of Smart Contracts 856 0
Inorder Tree Traversal without recursion 1133 14
Lambdas in C++ 843 11
Inorder Tree Traversal with recursion 892 25
C++ Count maximum number of consecutive ones in a vector 1622 10
C++ Parameter Binding 720 11
Python Converting a String to a List of Words 828 19
Python project to update a doc file conver it to the pdf file and send it to multiple email addresses 1914 29
Test article for interns Python Add Two Numbers 730 12
Ring Buffer 5689 11
Python 2.7 Run linux command and get output 1686 38
C++ Predicate 2859 10
Internship Opportunity at cppsecrets for Nov 2020 batch 5146 1
Find N pairs with smallest sums in two vectors 546 11
C++ Set of objects 911 21
C++ Selected Interns (Feb 2019 Batch - 1) 1200 15
Python challange for Sept 2019 Interns 2611 5
Write your own shared pointer 2918 10
Internship Opportunity at cppsecrets for May 2020 batch 10822 4
C++ Check whether two strings are anagram of each other 3742 12
C++11 Range based for loop 867 11
Erase elements from vector 2330 17
Python send mail 844 22
Git create branch 765 21
ssh Bad owner or permissions 992 0
C++11 decltype 572 12
C++ Read file and store it into integer variable 636 12
Python Tcp Server Example Code 911 12
C++ challange for Oct 2019 Interns 5450 0
Linux - Difference between tty and pts 12690 20
sudo error, is mode 0777, should be 0440 16 10
C++ Config Reader 9129 3
Find maximum product of three numbers 574 11
Python Converting a String to a List of Words based on spaces and alphanumeric characters 1095 25
Internship Opportunity at cppsecrets.com 4831 0
C++ map of char* and char* 5346 28
C++11 decltype and typeid 2705 16
Fork system call 967 15
Postorder Tree Traversal with recursion 852 21
How to give paid exam at cppsecrets.com? 1186 0
Functor in C++ 670 11
Internship Opportunity at cppsecrets for UPES Hackathon Participants 793 1
C++11 auto keyword 864 21
C++ Boost Serialization: Serializing primitive data types 55 3
C++ Validate parenthesis pattern 612 10
Dynamic Programming - Rod Cutting Problem 1618 20
Linux Print Process Tree 1083 27
Stipend update for the C++ interns of Feb 2019 2506 32
Linux Extract an RPM package without installing it 1049 16
Preorder Tree Traversal without recursion 844 16
Internship Opportunity at cppsecrets for March 2020 batch 8013 4
Python challange for Feb 2019 Interns 1150 27
UDP Client Program in C 4415 23
Deep Copy 912 11
Find the Nth largest element in a vector 6039 11
10 Most Frequent Laptop Issues and How to Solve them 539 0
Python Selected Interns (Feb 2019 Batch - 2) 1008 21
C++ challange for Interns (Feb 2019, Batch 2) 927 24
Select process by PPID 633 17
C++ typeid operator 3923 21
Python UDP client server example code 6002 29
C++ Abstract classes and pure virtual functions 792 23
fatal error: Python.h: No such file or directory 1488 0
Linux Find out how many file descriptors are opened 867 14
C++ Range based for loop 719 18
Binary Tree Traversal 3023 21
Python Command Line Arguments 1054 11
How to install Python Requests Library 741 13
Valgrind complete tutorial 1307 10
Function Pointer 655 10
C++ Is paragraph present in the file 722 22
How to cin to list 8557 10
C++11 Auto Example Code 787 14
Shallow Copy 2102 10
C++ File handling 1657 11
C++ challange for Feb 2019 Interns 1116 11
ps man page 808 19
errno.h Error Codes in Linux 951 15
Write copy constructor, assigment operator and desctructor of class having pointer variable 941 10
popen man page 987 15
C++ Read and write binary file 6989 22
C++ Generic file writer class 1285 22
C++ Object Slicing 678 14
OpenSSL generate hash of the file 2120 22
Singleton Class using C++ 11 5064 12
C++ challange for Interns May 2019 1546 21
C++ Pass local variable to Functor 812 10
Nested Template Class 1374 11
Python Sum of all the items in a list 588 11
How to give internship exam at cppsecrets.com 2604 1
C++ Selected Interns (Feb 2019 Batch - 2) 1048 19
Run unix command and get output 3542 28
Python Top MOST famous Python libraries and framework 855 29
Linux - find all the shell/terminals are running 904 29
Python challange for May 2019 Interns 1254 15
Linux Select Process By PPID 882 20
C++ File read and write 625 12
C++ Best way to trim std::string 6016 28
Build-in Functors 598 10
C++ Template with default arguments 789 20
How To Move Forward After Losing Your Job Due To Covid 366 0
Truminds 5G UPF now runs in 6WINDGate as a plugin 12737 2312

Comments