Thread: Random acces write to a binary file

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    77

    Random acces write to a binary file

    Hi,
    I'd like to have your expertise on writing in random mode - after setting the current pointer with fseek - into an existing binary file. I'm getting somehow confused with a bunch of options for fopen_s, including an "R" (for random), and a "c" to force "hard flushing" into the disk (bypassing the system's buffers), that I read about for the first time. What would be the right function to write - so that to avoid writing byte by byte?
    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Just a regular fopen() with a mode of "r+b" should suffice.

    Then seek to where you want to write, then fwrite() as much data as you please.

    Call fflush() if you switch back to reading the file.

    > I'm getting somehow confused with a bunch of options for fopen_s, including an "R" (for random), and a "c" to force "hard flushing" into the disk
    All of which is non-portable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    77
    I've tested the optimal configuration: fopen() with "wbc" mode and fwrite() as per the manual. Good performance since fseek() does automatically fflush() prior to any field pointer positionning into the file. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. How to store n write binary file?
    By megablue in forum C Programming
    Replies: 4
    Last Post: 10-23-2003, 03:53 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM