Thread: how can i seek in a file in binary mode?

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    18

    how can i seek in a file in binary mode?

    my problem is after i open a file in "rb" mode i want to start from beginning and read one byte at a time assign the current byte to a variable so i can perform operations on it and then save the result in a text file. and repeat the process until the end of file.
    i don't know how i can accomplish that.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You may not even need seeking to do what you describe. You could just read a byte at a time sequentially and write it out.

    If you do want to seek, though, see rewind(3): reposition stream - Linux man page

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    I have no idea what tutorial is talking about unless he's suggesting that you read the entire file in at once, modify the bytes, rewind the file, and then write out the entire file contents. But that's not what you asked about.

    Anyway, the main thing is that you need to open the file in "r+b" mode. r+ allows both reading and writing.

    If you really want to do it a byte at a time (which is kind of stupid, but whatever), then you can read a byte with fgetc, modify it, fseek back one position (-1 from SEEK_CUR), fputc the byte, fseek 0 from SEEK_CUR (a file positioning command is required to switch from writing to reading or vice versa), and continue looping. The loop would be controlled by the fgetc return value.

    Normally, we would read a block of bytes in at once, process that, then write it back out. That would mean you would use fread, modify the bytes, fseek back, fwrite, then the fseek 0 from SEEK_CUR, continue looping.

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    18
    Quote Originally Posted by algorism View Post
    I have no idea what tutorial is talking about unless he's suggesting that you read the entire file in at once, modify the bytes, rewind the file, and then write out the entire file contents. But that's not what you asked about.

    Anyway, the main thing is that you need to open the file in "r+b" mode. r+ allows both reading and writing.

    If you really want to do it a byte at a time (which is kind of stupid, but whatever), then you can read a byte with fgetc, modify it, fseek back one position (-1 from SEEK_CUR), fputc the byte, fseek 0 from SEEK_CUR (a file positioning command is required to switch from writing to reading or vice versa), and continue looping. The loop would be controlled by the fgetc return value.

    Normally, we would read a block of bytes in at once, process that, then write it back out. That would mean you would use fread, modify the bytes, fseek back, fwrite, then the fseek 0 from SEEK_CUR, continue looping.

    thanx i get it now. i'll get to writing it immediately

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by algorism View Post
    If you really want to do it a byte at a time (which is kind of stupid, but whatever)
    The stdio library (f* functions) already do buffering for you, so there is nothing wrong with processing one byte at a time. For example, if you want to capitalize every letter in the file, this is just fine (sans error checking):

    Code:
    int c;
        
    while ((c = fgetc(fp)) != EOF) {
      c = toupper(c);
      fputc(c, fp_out);
    }

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by c99tutorial View Post
    The stdio library (f* functions) already do buffering for you, so there is nothing wrong with processing one byte at a time. For example, if you want to capitalize every letter in the file, this is just fine (sans error checking):
    Sorry. I completely misunderstood the question. For some reason I thought he wanted to write the values back to the same file. You seemed to think that too with your mention of rewind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newline in binary mode
    By 843 in forum C Programming
    Replies: 4
    Last Post: 11-26-2010, 11:17 AM
  2. Replies: 15
    Last Post: 12-17-2008, 12:11 AM
  3. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  4. Replies: 3
    Last Post: 09-22-2005, 01:35 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 06:45 PM

Tags for this Thread