Do I need to wait in between fclose() and fopen()?

This is a discussion on Do I need to wait in between fclose() and fopen()? within the C Programming forums, part of the General Programming Boards category; Dear there, I am wondering whether a sleep(1) is necessary in the following pseudo code. Code: for (i=0;i<100000;i++){ 1. open ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    Do I need to wait in between fclose() and fopen()?

    Dear there,

    I am wondering whether a sleep(1) is necessary in the following pseudo code.
    Code:
    for (i=0;i<100000;i++){
    
    1. open a file, write new content to it, and close it
    2. system("sleep(1)");
    3. call a reader to read the file, and close it afterward.
    
    }
    My concern is that without the sleep(1), step 3 might not get the new content if the loop is executed really fast. However, with sleep(1), the code executed too slow. Furthermore, would such a high-frequent read/write of same file imposes a burden to the hard drive, or even affect other users on the server?

    All the best,
    Michael

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    >> I am wondering whether a sleep(1) is necessary in the following pseudo code.

    sleep(1) will have negligible effect on the speed of your program. The system function already suspended your program to execute sleep and come back. This is not the way to make sure fopen and fclose have worked properly. I suspect that normal file opening and closing will work as expected, as long as you know how to check for errors.

    >> Furthermore, would such a high-frequent read/write of same file imposes a burden to the hard drive, or even affect other users on the server?

    I dunno tbh. I wonder why you have to do it so many times.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    Is it necessary? No.
    Can it be a burden? Probably not the functions themselves. A usual implementation of the functions would usually buffer data before writing it, to minimize the impact on the hard drive writing small chunks of data. So this means that you can run into trouble if you're writing too much data from too many processes at one time. But that's not the fault of the write functions.
    But it cannot be said for certain since different implementations may differ, and there are external factors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    35

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    What are you trying to say with fflush?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Output files are flushed before they are closed, but OP wouldn't have to open and close so much if he just kept it open and flushed manually.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    That detail slipped by me. Of course, if you're going to frequently read/write to the same file, you would certainly face penalties for opening/closing it frequently.
    For a server, it would probably be best to avoid opening/closing and flushing, to avoid trashing of the hard drive.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The sleep should not be necessary. But at the same time, you should not have to open and close the same file repeatedly like that. Once you have a FILE* stream open, you can jump around in the file with ftell() and fseek(). And if you want to read and write to the same FILE* stream, you can: just open the file in a "+" mode like "w+". For details about the valid file modes see fopen().

    Important note: "when you switch between reading and writing [to a FILE* stream], there must be an intervening fflush, fsetpos, fseek, or rewind operation." (From fopen, _wfopen (CRT))

    I'm not sure what you're trying to do there, but it seems like you might be able to achieve it by writing some stuff out, rewind()ing or fseek()ing somewhere else in the file, reading more data in, etc. Just remember to open the file in a "+" mode and fflush() or whatever when switching between reading and writing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fclose slow
    By Rmantingh in forum C Programming
    Replies: 4
    Last Post: 04-08-2010, 11:01 AM
  2. fopen with std::string
    By Ducky in forum C++ Programming
    Replies: 44
    Last Post: 08-03-2009, 01:55 PM
  3. fopen and fclose functions
    By anilk in forum C Programming
    Replies: 20
    Last Post: 01-31-2006, 03:31 PM
  4. fopen, fclose
    By Denethor2000 in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2004, 10:11 PM
  5. fclose()
    By GanglyLamb in forum C Programming
    Replies: 4
    Last Post: 11-11-2002, 07:45 AM

Tags for this Thread


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