Thread: Writing to Disk

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    129

    Writing to Disk

    I've finally cleaned out all my obvious bugs except this one. When I try to write to a w+ file, the file is overwritten with... nothing. I.e., the file is successfully opened and the string to be written contains printable characters (I checked with printf), but nothing is actually sent to the file.

    Here's the function that tries to write, which is also the one I checked the string in.
    Code:
    void outline_write(struct outline *this, FILE *file){
        char *text = outline_item_toString(this->root, 0);
        fprintf(file, "%s", text);
        free(text);
    }
    Is there any other code that might hold a clue, or am I doing something blatantly wrong in those three lines? Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you have the string enclosed in double quotation marks on both ends of it?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    are you closing file before exemining its contents?
    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

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    Adak, the string is a composite of user input, with indentation and outline numbering added by the program. The program compiles cleanly.

    Vart, the program doesn't examine the file; I use gedit to detect the update. (I really should have mentioned that originally.) Is this an inaccurate method?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Jesdisciple View Post

    Vart, the program doesn't examine the file; I use gedit to detect the update. (I really should have mentioned that originally.) Is this an inaccurate method?
    I mean - when you exemined file with editor - has the program already closed the file? OS could delay flushing changes to disk till the file is closed
    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
    Aug 2008
    Posts
    129
    LOL... As I was thinking about your post, I realized I didn't call fflush. I quit the program and switched back to gedit, and sure enough there are my data.

    I wonder what would be necessary to safely allow concurrent editing, other than flushing the stream? I'm in read-write mode, so shouldn't I be able to read the changed file without reopening it?

    Thanks so much!

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Jesdisciple View Post
    I'm in read-write mode, so shouldn't I be able to read the changed file without reopening it?
    Have you read the manual?
    http://www.cplusplus.com/reference/c...dio/fopen.html
    For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.
    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

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    I have, but I obviously lost some of it (and it's been a while).

    Thanks for the help! Now I have to deal with the new flood of read-related bugs, of course... (Side note: I had to change the mode to r+ so there would still be something to read.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux writing often on main disk
    By mynickmynick in forum Linux Programming
    Replies: 17
    Last Post: 04-07-2009, 02:05 PM
  2. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  3. Writing Directly to Hard Disk Sectors
    By ali_aslam in forum Linux Programming
    Replies: 2
    Last Post: 07-27-2007, 02:29 AM
  4. Help with writing a disk filler applet
    By mlupo in forum C Programming
    Replies: 8
    Last Post: 01-18-2006, 02:39 PM
  5. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM