0

I want to learn how to search in the file by passing the pointer of the stream to a class.

I can successfully get the first character from the file using std::fstream and std::filebuf*

char symbol;
std::fstream by_fstream;
by_fstream.open("First_test_input.txt");

std::filebuf* input_buffer = by_fstream.rdbuf();
symbol = input_buffer -> sbumpc();

std::cout << "\nSymbol that get from a file by rdbuf(): " << symbol;

Output: Symbol that get from a file by rdbuf(): M

But I'm not sure how can I send any pointer to my original stream of the file from main to a class.

Ideally, it would be great to do something like this:

#include <iostream>
#include <fstream>

class from_file
{
public:
    
    char c;
    
    from_file () {
        std::cout << "\nCharacter that get from file by to class variable"
                    <<" then printed: " << c;
    };

    from_file (char *pointer){
        c = pointer -> sbumpc();
    };

    ~from_file ();
    
};

int main(){

    std::fstream by_fstream;
    by_fstream.open("First_test_input.txt");


    std::filebuf* input_buffer = by_fstream.rdbuf();
    from_file send(&input_buffer);
    from_file show;

    return 0;

}

Looking for advice on where I can find documentation about similar headers to do a such task.

  • First of all, you should pass a reference to the stream instead of the buffer pointer. Secondly the type of `&input_buffer` is `std::filebuf**` which is vastly different from the `char*` argument the overloaded `from_file` constructor expects. Thirdly, why do you print the *uninitialized* variable `c` in the `from_file` default constructor? Fourthly, why do you create two independent and separate objects? – Some programmer dude Apr 22 '22 at 16:05
  • I think the main problem isn't related to the files, streams and buffers, but rather that you seem to have skipped some chapters or lectures about the basics of C++ and objects. I recommend you invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and read them from the very beginning. – Some programmer dude Apr 22 '22 at 16:05

1 Answers1

0

You are going about this all wrong.

First off, you should pass around (a reference to) the stream itself, not its internal buffer. Use std::istream methods like read() or get() or operator>> to read from the stream, let it handle it own buffer for you.

Secondly, you are trying to make a 2nd completely separate object "magically" know what a previous object is holding. That is not going to work out the way you want, either.

Try something more like this instead:

#include <iostream>
#include <fstream>

class from_stream
{
public:
    
    char c;
    
    from_stream (std::istream &in){
        c = in.get();
        // or: in.get(c);
        // or: in.read(&c, 1);
        // or: in >> c;
    };

    void show() const {
        std::cout << "\nCharacter that get from file by to class variable"
                    <<" then printed: " << c;
    }
};
    
int main(){

    std::ifstream by_ifstream;
    by_ifstream.open("First_test_input.txt");

    from_stream send(by_ifstream);
    send.show();

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Wow! You rock! I really miss some stuff in learning and also I studied by myself a long ago (4 years ago), then was interrupted. Now I want to catch up and definitely will read about c++ from the beginning. But answers like yours are really inspiring to learn more. – Dmytro Kovryzhenko Apr 22 '22 at 17:01
  • "*I ... definitely will read about c++ from the beginning*" - then make sure you are learning from [good quality C++ books](https://stackoverflow.com/questions/388242/). – Remy Lebeau Apr 22 '22 at 17:18
  • Yes, sure, thanks! – Dmytro Kovryzhenko Apr 22 '22 at 18:37