Thread: Struct File and array problem Please help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're mixing your output and input modes.

    Use fprintf() for text mode, and you can use fscanf(), (or any other text mode file reader), to read the data back into your program.

    Use fwrite() to write out data in binary mode, and use fread() to read it back into your program.

    It's always good to be specific when you open files:

    Not just mode "r" for reading. Either "rt" or "rb", please. Be specific, either text or binary. Don't leave it up to some global variable buried in a header file, somewhere.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Adak View Post
    Not just mode "r" for reading. Either "rt" or "rb", please. Be specific, either text or binary. Don't leave it up to some global variable buried in a header file, somewhere.
    rt is non-standard extention

    r - is standard for text mode
    rb is standard for binary mode.

    To the OP:

    Also fflush(stdin) is undefined - read FAQ. FAQ has samples how to get rid of junk in the input buffer using standard ways

    gets should not be used - we have FAQ on the issue.


    feof should not be used to control loops. FAQ explains why
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Also, if you write the data in file member-by-member in binary, it is possible you'll be bitten by struct padding.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    On what compiler do you get an error with file mode "rt"?

    I use three compilers and have not noticed any problem with it.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Adak View Post
    On what compiler do you get an error with file mode "rt"?

    I use three compilers and have not noticed any problem with it.
    And it makes this mode standard in your opinion?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by vart View Post
    And it makes this mode standard in your opinion?
    If it works better, I tend to use it. Portability not only means the current compilers, but also the legacy compilers, if you have programs created for them.

    I do, so I use what works best for me. I recommend it here, because a lot of the students are still using the legacy compilers.

    Did you notice the conio.h include file, Vart? That's pretty much a gimme that he's using Turbo C, and my advice is targeted at Turbo C users (like me, sometimes).
    Last edited by Adak; 04-01-2009 at 02:52 AM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Surprised that the compiler did not barf when it came across *data[i] as in.
    Code:
    fwrite(*data[i],sizeof(data[i]),1,clientFile); /* first arg should be a void pointer */
    Instead of testing for feof() on the stream, a better test is to terminate if no. of items read is less than requested.
    Code:
    while(!feof(clientFile))  ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  2. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM