Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: [C] Write to a file some binary data, read some binary data from a file?

  1. #1
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    [C] Write to a file some binary data, read some binary data from a file?

    As title says, how? Post me some tutorials please.

    Can't find any with google or even dogpile.
    Keyboard not found!

    Press any key to continue...

  2. #2
    Join Date
    Feb 2008
    Beans
    1,031

    Re: [C] Write to a file some binary data, read some binary data from a file?

    theres loads out there, google "c input output"

    e.g. http://www.cs.bu.edu/teaching/c/file-io/intro/

    thinking about it google binary input output instead....

    e.g. http://www.cprogramming.com/tutorial/cfileio.html

    hope that helps

  3. #3
    Join Date
    May 2006
    Beans
    471

    Re: [C] Write to a file some binary data, read some binary data from a file?

    I would say you should get familiar with GNU's libtool. You can find it out at http://www.gnu.org/software/libtool/manual/ .... It may be hard to read at times in certain sections, but it is a great resource to learn about the C-Standard.
    t.t.d.
    Segmentation Fault

  4. #4
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C] Write to a file some binary data, read some binary data from a file?

    Have you read on fwrite() and fread()?

  5. #5
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    Re: [C] Write to a file some binary data, read some binary data from a file?

    I know how to save/load a single integer, but how to do it with a linked list, that might have hundreds of nodes?
    Keyboard not found!

    Press any key to continue...

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,274
    Distro
    Kubuntu

    Re: [C] Write to a file some binary data, read some binary data from a file?

    I linked list generally has one or more data values, and a pointer link to the next node (and sometimes the previous node).

    Storing the pointer (that is, the address) within a file to read at a later time is not practical because the application reading in the data will not be able to use the pointer (it may lay outside its program space).

    Thus I recommend that you store only the data value(s), in a contiguous format within the binary file. To do this, you will need to traverse your linked list from the beginning to the end.

    If your data is represented by a structure, then it is easier to write the data to the file. If they are independent fields, then a write may be needed for each field. Similarly when it comes to read in the data.

    P.S. Concerning the write and read info above in the last paragraph, there are ways to write/read only the node to a file, minus the pointers. Just use arithmetic when writing/reading. For instance:

    PHP Code:
    struct Node
    {
      
    int          field1;
      
    int          field2;
      
    struct Nodenext;
    };

    ...

    struct NodemyNode = ...;

    write(fdmyNodesizeof(struct Node) - sizeof(struct Node*));   // subtract the size of the field 'next' 
    Last edited by dwhitney67; November 14th, 2008 at 09:37 PM.

  7. #7
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C] Write to a file some binary data, read some binary data from a file?

    @dwhitney: Is there any advantage on using read()/write() compared to fread()/fwrite()?

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,274
    Distro
    Kubuntu

    Re: [C] Write to a file some binary data, read some binary data from a file?

    Quote Originally Posted by nvteighen View Post
    @dwhitney: Is there any advantage on using read()/write() compared to fread()/fwrite()?
    Not that I am aware of. I am by no ways an expert at *nix internals.

  9. #9
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    272

    Re: [C] Write to a file some binary data, read some binary data from a file?

    Quote Originally Posted by nvteighen View Post
    @dwhitney: Is there any advantage on using read()/write() compared to fread()/fwrite()?
    The only reason I'd ever use read() over fread() is because it has a non-blocking mode. But read() and write() are for UNIX only, where as fread() and fwrite() are part of the ANSI/ISO C standard.

  10. #10
    Join Date
    Sep 2007
    Location
    Cambridge, MA
    Beans
    635

    Re: [C] Write to a file some binary data, read some binary data from a file?

    Quote Originally Posted by nvteighen View Post
    @dwhitney: Is there any advantage on using read()/write() compared to fread()/fwrite()?
    The f* stuff goes through C stdio. The !f* stuff works on Unix file descriptors.

    That means it is buffered. Unsuitable for network sockets (unless you know how to control it). Generally faster for files, terminals and the like.

    Serializing a Lisp in C is a pain, not to mention you need to serialize the objects that are inside the list, too. If they are all of the same non-pointer data type I'd convert the list to an array before saving it.

    In general, if you have a list of complex objects that isn't easy to pack without pointers it's start to think about XML or SQL.
    Last edited by Cracauer; November 15th, 2008 at 05:42 PM.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •