Need Help with file handling

hello everyone, l am learning programming on my own and l was watching tutorial on youtube about "File Handling" and l was wondering how we can read and write in files working with "OOP" technique, l mean how can we work with files using Classes or if l am inheriting classes and how can i write and read that data into file.

l am finding this over internet but can't find any solution.So seeking for help from you.

any type of help would be appreciated.
Files cannot be OOPed. They are streams.

The C++ Standard Library provides OOP-like objects for handling files. Use them.

Here's a simple example of reading and writing lines from/to file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iostream>
#include <string>

// Copies input stream to std::cout, line by line
void display( std::istream& ins )
{
  std::string s;
  while (getline( ins, s ))
    std::cout << s << "\n";
}

int main( int argc, char** argv )
{
  if (argc > 1)
  {
    // Display file named as argument
    std::ifstream f( argv[1] );
    display( f );
  }
  else
    // Display data from stdin
    display( std::cin );
}

Good luck!
A class can write to a file. Often a class will self-serialize, or convert itself into a vector of bytes that can be written to a file and may even have a constructor or loader that can fill it back in reading from a file.

From there you can chain the same concepts to fit your need ... a parent class may need to call the method of its child to get a vector of bytes to write that represent the child and may need to recreate its children when reading from a file, to however deeply nested your construct is and what the file format looks like and what the needs of the program actually are, etc. Or you can avoid chaining and have each object handle its own I/O in its own file when that makes more sense.

Is this what you are asking?
Topic archived. No new replies allowed.