Thread: r+ and w+

  1. #1
    Unregistered
    Guest

    r+ and w+

    What is the difference between these two modes of writing to a file?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    r+ simply opens the file for both reading and writing.
    w+ opens the file for reading and writing, but discards the contents of the file or creates the file if it doesn't exist.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    Thanks, Prelude. Hope I didn't offend you on Saturday. You sure know your stuff!

  4. #4
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    In K&R w and w+ have the same description except for on word. w writes and w+ updates. What is the difference?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Hope I didn't offend you on Saturday
    About what?

    >In K&R w and w+ have the same description except for on word.
    >w writes and w+ updates. What is the difference?
    K&R pp.242

    FILE *fopen(const char *filename, const char *mode)
    fopen opens the named file, and returns a stream or NULL if the attempt fails.
    Legal values for mode include

    "r" open text file for reading
    "w" create text file for writing; discard previous contents if any
    "a" append; open or create text file for writing at end of file
    "r+" open text file for update (i.e., reading and writing)
    "w+" create text file for update; discard previous contents of any
    "a+" append; open or create text file for update, writing at end

    Update mode permits reading and writing the same file; fflush or a file-positioning function must be called between a read and a write or vice versa. If the mode includes b after the initial letter, as in "rb" or "w+b", that indicates a binary file. Filenames are limited to FILENAME_MAX characters. At most FOPEN_MAX files may be open at once.
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    Thanks, I should have read further.

Popular pages Recent additions subscribe to a feed