Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Writing a struct to file

  1. #11
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Corrections
    Quote Originally Posted by crosswire
    fwrite is an appropriate function to use instead of fprinft because it writes byte by byte but it still suffers from text mode translation. fprintf can write binary using the format string and some casting but it looks a bit indirect.
    Corrections, fprintf cannot be used. I was thinking of casting the struct to a string, then writing the string, but only a part of the string would be written because a null charater may terminate the string. A string will not be read properly if it holds binary data
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  2. #12
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by pogi_2nr
    Portability isnt a real concern for most people. It bothers me greatly as the C
    programs I write normally have to compile and run on windows and a slew of
    other unices. Getting the code to compile and run isnt really much trouble but
    having it read data files from other platforms is always a concern.

    For the scenario presented in this thread I would suggest using unsigned 16bit integers
    along with the following function to save the data before writing.
    Code:
    uint16_t htons(uint16_t hostshort)
    htons stands for host to network short, basically a function used for packing data for network traffic.

    and when you are ready to read the file again unpack it via ntohs()
    or to simply put it you write the file in network byte order. and read it as such.

  3. #13
    Join Date
    Jun 2003
    Posts
    453
    Rep Power
    0

    Default

    I have never seen that b4 today pogi. thanks for the info.
    We Is Friends!
    Me And You Is Friends!
    You Smile, I Smile ....
    You Hurt, I Hurt ..
    You Cry, I Cry ..
    You Jump Off A Bridge ..
    I Gonna Miss Your E-Mails !

  4. #14
    Join Date
    Sep 2005
    Posts
    55
    Rep Power
    0

    Default

    That's why you should use Java, Bill!

    ;-)

  5. #15
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    blaspheme, take that talk out of the C forum!

  6. #16
    Join Date
    Jun 2003
    Posts
    453
    Rep Power
    0

    Default

    Quote Originally Posted by pogi_2nr
    blaspheme, take that talk out of the C forum!
    tell him again... away with Ja#$ from this thread .
    We Is Friends!
    Me And You Is Friends!
    You Smile, I Smile ....
    You Hurt, I Hurt ..
    You Cry, I Cry ..
    You Jump Off A Bridge ..
    I Gonna Miss Your E-Mails !

  7. #17
    Join Date
    Sep 2005
    Posts
    55
    Rep Power
    0

    Default

    Ok, ok, I'm out of here ...


  8. #18
    Join Date
    Oct 2004
    Posts
    393
    Rep Power
    0

    Default

    lol. Thanks for all the support. U guys are all too advance - now am confused . Not to fear, ive taken in account what crosswire had said in another thread and modified the program accordingly. It work like a charm. At least i know where to take with my probs .

    P.S. cant wait to get to u guys level
    Moving forward never back. That's my way of life.

Posting Permissions

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