View string from a certain offset in a file

Hello everyone, I been practicing with the filestreams in c++ and I can successfully get the string within a binary file to show on screen. However, the issue I am having is that I only want it to show strings from a certain offset to an end offset.

This is the code I have to display all the strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <cstring>


int main()
{
    std::fstream file("README.bin",std::ios::binary | std::ios::in);

    file.seekg(0);
    char ch;
    while (file.good()){

        file.get(ch);
        std::cout << ch;
    }

    return 0;
}


The file itself is just a test .bin file. It displays all characters but I only want it to display for example 'test file' and not 'test file @#zxds#' etc.

I'm guessing my issue lies within the seekg method but can't figure it out.

I appreciate any help :)
Replace 0 in seekg with offset you need.

For example file, seekg(100) will allow you to read info starting from 101th byte
Thank you :)

The only other thing I'm trying to figure out is how to set an end offset so it doesn't continue to the end of the file?
Read only needed amound of bytes. end offset - start offset = amount of bytes to read.
Thank you once again :)

Marking as solved.
Topic archived. No new replies allowed.