Writing to Disk

This is a discussion on Writing to Disk within the C Programming forums, part of the General Programming Boards category; I've finally cleaned out all my obvious bugs except this one. When I try to write to a w+ file, ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    are you closing file before exemining its contents?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 01:39 PM
  5. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21