keeping FILE* object open

This is a discussion on keeping FILE* object open within the C Programming forums, part of the General Programming Boards category; I use visual studio and standard C for programming. The program I am writing uses winsock, too. I used to ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    keeping FILE* object open

    I use visual studio and standard C for programming. The program I am writing uses winsock, too. I used to open file with fopen "w", but the program encounters exception if I open file and use fprintf or fclose after the long time waiting for socket. This exception doesn't happen if I open file and immidiately write and close (fopen,fprintf,fclose sequentially).

    Why?


    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you have software that closes open sockets after a certain length of time, if they're not being used? I'm thinking of firewall type programs, in your operating system.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by Adak View Post
    Do you have software that closes open sockets after a certain length of time, if they're not being used? I'm thinking of firewall type programs, in your operating system.
    I've no problem in socket itself, it works just fine. I've problem in FILE operation that has no relation with socket. FILE stays open for a nearly long time only.

    Thanks!

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Are you sure you're using it correctly, and are you sure they are what's causing the problem (and not something else)? There are a few things you ought to look out for:

    — Check the return value of fopen.
    — Ensure that you're passing the correct arguments to those functions.

    If you're logging data to a file infrequently, then it might be beneficial to only open the file (in append mode) when you need to log data, and close it immediately after. I don't really see how having an opened file could be a problem, since stdin, stdout, and stderr are typically open for your entire program's interpretation unless you close them manually. You aren't using winsock functions with FILE objects, are you?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > but the program encounters exception if I open file and use fprintf or fclose after the long time waiting for socket.
    Have you considered FINDING the bug in your present system, rather than trying to find an alternative solution (which may still have the bug) which doesn't exhibit the problem.

    Run the debug build in the debugger, and just wait for it to crash. It will point you at the location where it crashed (you might have to traverse the call stack to locate the caller in your code), but at least it will give you some ideas to think about.

    You've got a bug with a repeatable cause - be thankful for small mercies.

    What really irritates people who code for a living are bugs with no repeatable cause at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by Barney McGrew View Post
    Are you sure you're using it correctly, and are you sure they are what's causing the problem (and not something else)? There are a few things you ought to look out for:

    — Check the return value of fopen.
    — Ensure that you're passing the correct arguments to those functions.

    If you're logging data to a file infrequently, then it might be beneficial to only open the file (in append mode) when you need to log data, and close it immediately after. I don't really see how having an opened file could be a problem, since stdin, stdout, and stderr are typically open for your entire program's interpretation unless you close them manually. You aren't using winsock functions with FILE objects, are you?
    The error doesn't persist. I guess it was some problem resolved by restarting. Maybe...


    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open file name object
    By JJFMJR in forum Windows Programming
    Replies: 3
    Last Post: 09-14-2007, 06:44 PM
  2. Keeping window open
    By jpipitone in forum C Programming
    Replies: 5
    Last Post: 12-01-2003, 12:56 PM
  3. Problem keeping Dos Window Open
    By Unrege 123 in forum C++ Programming
    Replies: 3
    Last Post: 07-31-2003, 12:32 PM
  4. Keeping the console open.
    By foogoo in forum C++ Programming
    Replies: 5
    Last Post: 10-30-2002, 07:43 PM
  5. Keeping the console window open
    By Adock in forum C Programming
    Replies: 3
    Last Post: 01-31-2002, 05:15 AM

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