reading a writing x bytes

This is a discussion on reading a writing x bytes within the C Programming forums, part of the General Programming Boards category; I want to write a cross platform program that reads x bytes from a file, then writes x bytes to ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    13

    Question reading a writing x bytes

    I want to write a cross platform program that reads x bytes from a file, then writes x bytes to a file. I know how to do this under Linux. I would simply use:

    #include <unistd.h>
    write()
    and
    read()

    Is there something on the fopen family I could use? Sorry if this is a stupid question.

    TIA
    #define punkCow

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    #include <stdio.h>

    fopen : open the file
    fgets, fscanf, fread : different function for getting the data
    fprintf, fwrite : different function for writting the data
    fclose : close the file


    Now look that these function, write some code, and then post it so we can help you with any problems.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    13
    Originally posted by Thantos
    #include <stdio.h>

    fopen : open the file
    fgets, fscanf, fread : different function for getting the data
    fprintf, fwrite : different function for writting the data
    fclose : close the file


    Now look that these function, write some code, and then post it so we can help you with any problems.
    Do these functions let me read x numbers of bytes, or x charcaters? I'm looking for something that will let me read x numbers of bytes form a file (I guess thats stupid now that I think of it... will fgetc read one byte at a time?) *runs off to write some code*
    #define punkCow

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use fread() and fwrite(). And make sure you open the file in binary mode.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    13
    Originally posted by swoopy
    Use fread() and fwrite(). And make sure you open the file in binary mode.
    Thank you! I love it here, I can't say how happy it make me to see programming warriors helping the newbies like me :-D

    Thank you everyone that has ever helped a newbie! :-D
    #define punkCow

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Use fread() and fwrite(). And make sure you open the file in binary mode.
    Not a good idea if the data files are intended to be used in a cross platform way as well as the program itself. fread and fwrite do what you would expect: work with a one-to-one mapping of memory and data file. This makes the files themselves as non-portable as it gets.

    If the OP doesn't need to move the files around, fread and fwrite will suffice, but if the files do need to be moved around, a more robust solution is required. Unfortunately, that means writing your own binary I/O functions that make sure the data is written the same way everywhere. I touched on this issue here.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    13
    Originally posted by Prelude
    >Use fread() and fwrite(). And make sure you open the file in binary mode.
    Not a good idea if the data files are intended to be used in a cross platform way as well as the program itself. fread and fwrite do what you would expect: work with a one-to-one mapping of memory and data file. This makes the files themselves as non-portable as it gets.

    If the OP doesn't need to move the files around, fread and fwrite will suffice, but if the files do need to be moved around, a more robust solution is required. Unfortunately, that means writing your own binary I/O functions that make sure the data is written the same way everywhere. I touched on this issue here.
    Does fputc and fgetc read/write one byte at a time? Would fgetc and fputc do the trick on a file opened in binary mode, or ould I still have to issue of not being cross platform?

    Thank you, I'm a little slow ;-)
    #define punkCow

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Does fputc and fgetc read/write one byte at a time?
    Yes.

    >Would fgetc and fputc do the trick on a file opened in binary mode
    No. You would still have the problem of size, byte order, floating-point format, padding, etc... But if you can get away with using text files instead of binary files then all of these problems just go away.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  2. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 01:28 AM
  3. Pipe: writing and reading.
    By apacz in forum C Programming
    Replies: 0
    Last Post: 06-07-2006, 12:12 PM
  4. Reading Individual bits from 2 bytes
    By Giania in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 03:16 AM
  5. reading from the open socket then writing to a file...
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-06-2002, 05:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21