Thread: fclose()

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110

    Exclamation fclose()

    i have a console apllication that uses fopen() it opens a txt file for
    reading and writing . Now if i close the application before its completely finished the txt file hasnt been closed because i do fclose() completely at the end (what is necessary because it reads data so the program can run).
    Now what happens if i close the application before fclose() then the txt file is still in use according to my comp is there a way to fclose() when the application is aborted before the end of the whole application?
    i know this question mayb confusing in some way ....

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The OS should close the file for you, but it won't flush any output buffers, so you'll probably loose data.

    Not fclose()'ing a file is bad practice, if its by happening by design, you need to rethink it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110

    Wink

    thx...
    ill just ad a readme file saying dont close the app before its finished that looks the easiest way for now

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by GanglyLamb
    thx...
    ill just ad a readme file saying dont close the app before its finished that looks the easiest way for now
    What is "stopping" the app? Is the user hitting CTRL+C or something?

    Also, you can fflush(fp); every so often to help prevent data loss.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    if the user hits ctrl c its no problem its only a problem when the user exits the app by pressing alt-ctrl-del and close it that way or just pushes the button with the cross in the right corner and then press yes when it gives a warning. That warning is standard its something like data might be lost dunno wuts it in english anyway if you close it like that then the os thinks the file is still open.


    edit==>also if you close the app by doing one of those two things then the next time u run the app it will say that the .txt files cant be read wich is an error done like this
    Code:
    hscr=fopen("data\\hscr.txt","r+");
    		if(hscr==NULL){                                						 printf("The file hscr.txt couldnt be opened or something like that blabla\n");
    						 }
    and since i need data from that file to run the app he wont do a thing because he cant open the .txt file
    and then the only choice u have is to reboot the OS
    becaus then the hscr.txt will be closed
    Last edited by GanglyLamb; 11-11-2002 at 08:51 AM.

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. pclose() vs fclose()?
    By cpjust in forum C++ Programming
    Replies: 14
    Last Post: 12-21-2007, 12:46 PM
  3. Question about fclose
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 01:31 AM
  4. quick fclose question
    By ihatejava in forum C Programming
    Replies: 5
    Last Post: 10-10-2002, 03:56 PM
  5. close and fclose
    By Ian in forum C Programming
    Replies: 4
    Last Post: 12-13-2001, 05:31 PM