3

I have a binary file whose size is "x+a+y" bytes. I have some questions;

  • Without exhaustive reading x bytes, How can I jump to start point of a bytes ?
  • Without taking all of the information reside in a line and then doing required modification and finally writing new information to the same line, How can I just change "a bytes" of the data in place whose starting address is shown as P?
  • Can I put a pointer to a starting point of "a bytes" that is to P ?

Question about how data is stored in pile file;

  • Why is all of the primitive data type ( such as int ) stored in pile file preferred to be unsigned ?
  • Why is offset always chosen as unsigned int ? eX: x,a,y are known value

    ___________________________________________________________
    |                             |     |                     |
    |                             |     |                     |
    |----------> x <--------------|> a <|-------> y <---------|
                                  ^
                                  ^
                                  P
    

Platform linux

Note: If I do dublicate question, please inform me so that I can delete this question. Feel free when you command smth

1
  • [Find Working Code for Fetching, modifying information on binary file in the link:][1] [1]: stackoverflow.com/a/12406355/559746 Commented Sep 13, 2012 at 12:39

1 Answer 1

1

There is a method in ifstream called seekg that moves the read-position to a specific place in the file. See http://en.cppreference.com/w/cpp/io/basic_istream/seekg.

Sign up to request clarification or add additional context in comments.

5 Comments

seekg is for reading and seekp for writing which is what he needs.
If he need to both read and write, using fstream then it uses the methods from istream for seaking.
But seekg only works in limited ways. Above all, to seek to an arbitrary position, the file must be opened in binary mode. (Otherwise, you can only seek to a position which you've obtained by tellg.) And of course, depending on the file type, seekg might not work at all (e.g. if the file is a terminal).
@JamesKanze The title of the question contains the words "binary file", so I guess the file will be open in binary mode, in which case seekg is good to skip X bytes at the beginning of the file.
@JoachimPileborg That seems obvious to you and me, but it seems worth mentioning to someone who's also unaware of the existence of seekg.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.