View string from a certain offset in a file
Oct 29, 2015 at 11:47am UTC
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:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <iostream>
std::fstream file( "README.bin",std::ios::binary | std::ios::in);
std::cout << ch;
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 :)
Oct 29, 2015 at 2:59pm UTC
Replace 0 in seekg with offset you need.
For example file, seekg(100) will allow you to read info starting from 101th byte
Oct 29, 2015 at 3:20pm UTC
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?
Oct 29, 2015 at 3:23pm UTC
Read only needed amound of bytes. end offset - start offset = amount of bytes to read.
Oct 29, 2015 at 3:31pm UTC
Thank you once again :)
Marking as solved.
Topic archived. No new replies allowed.