Thread: Function to write two bytes at a time to a file?

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    40

    Function to write two bytes at a time to a file?

    First, two bytes are read and combined at a time from a file, so 7F FF (two separate values) becomes one value, 7FFF. That part has already been done. However, I'm looking for a means of injecting those bytes into a new file. I've tried fputc, but that only injects one character at a time. I've also tried fputs, but that only works for strings; the values are stored as integers in this case.

    Any tips for injecting these double-byte values into a file? Help is much appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How do you assemble say 0x7FFF out of two bytes 0x7F and 0xFF - using << and | perhaps (or *256 and add)

    Simply pick it apart in the reverse way, and write some bytes.
    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
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Is the file to be text or binary? If it's a binary file, use fwrite...

    Cprogramming.com - Tutorials - C File I/O

    Kevin

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Flotonic View Post
    First, two bytes are read and combined at a time from a file, so 7F FF (two separate values) becomes one value, 7FFF. That part has already been done. However, I'm looking for a means of injecting those bytes into a new file. I've tried fputc, but that only injects one character at a time. I've also tried fputs, but that only works for strings; the values are stored as integers in this case.

    Any tips for injecting these double-byte values into a file? Help is much appreciated!
    Look at fprintf() in your library documentation...

    If you have the 2 byte value in a short int you can easily print it to the file as decimal, hex, or octal.
    If it's in a string you can just print it directly to the file.

    Really... this isn't hard.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    40
    Salem and kmess were more understanding of how the question was asking about binary stuff, not text. Thanks for the tips!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Flotonic View Post
    Salem and kmess were more understanding of how the question was asking about binary stuff, not text. Thanks for the tips!
    In that case just use fread() and fwrite()...

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    40
    Just finished giving fwrite(); a shot. I get "passing argument 1 of fwrite makes pointer from integer without a cast." I think that fwrite is made for putting down down things declared with "char," not "int."

    I could always try making a loop to split each value into two smaller values that can fit into single-spaced "char" variables.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    fwrite expects you to tell it where it is, not what it is. (EDIT: At the same time, it also expects you to be using data types that make sense. If you only want to write two bytes out to a file, then using a four-byte data type will be Frowned Upon. If you have to write a specific number of bytes, then a char array is probably your best option.)

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Flotonic View Post
    Just finished giving fwrite(); a shot. I get "passing argument 1 of fwrite makes pointer from integer without a cast." I think that fwrite is made for putting down down things declared with "char," not "int."

    I could always try making a loop to split each value into two smaller values that can fit into single-spaced "char" variables.
    Try it like this...

    Code:
    short myint;
    
    fwrite(&myint, sizeof(myint), file);
    fWrite wants the address not the value.

    EDIT: Sorry Tabstop... I didn't see your post till after sending this...

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Flotonic View Post
    I've tried fputc, but that only injects one character at a time. I've also tried fputs, but that only works for strings; the values are stored as integers in this case.
    Why does it matter if you call fputc twice or fwrite once?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What matters is that fwriting two bytes at once exposes you to endian issues.
    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.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    What matters is that fwriting two bytes at once exposes you to endian issues.
    It sounds like he has that covered though:
    Quote Originally Posted by Flotonic View Post
    First, two bytes are read and combined at a time from a file, so 7F FF (two separate values) becomes one value
    He already knows the order he needs to read them, so writing them should be the same exercise in reverse. It's not like he's going to magically make this portable by writing two bytes at once instead of one at a time.

    I'm just not sure why writing one byte two times confused him more than reading one byte two times.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write the low-order 8 bits of the time to the disk file
    By kfuller002 in forum C Programming
    Replies: 2
    Last Post: 11-10-2010, 02:32 PM
  2. Read/Write file at the same time
    By doia in forum C Programming
    Replies: 2
    Last Post: 02-25-2010, 04:18 PM
  3. a function that returns the number of bytes of a file
    By paulovitorbal in forum C Programming
    Replies: 22
    Last Post: 05-15-2006, 06:01 PM
  4. How to get the last write time of a file?
    By maxorator in forum Windows Programming
    Replies: 1
    Last Post: 12-12-2005, 09:52 AM