Thread: error signal handling

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Question error signal handling

    Hello boys!
    I've got a problem with the handling of OS signal error while opening non-existing files... Look at my little program I've attached: it tries to open a file which doesn't exist (I want to update it, so I used "r+"); Windows gives an error signal (invalid access memory), so I wrote a function to handle it (signal_handler). But if the user chooses to continue after the error, Windows keeps giving the error signal, so the program enters into a sort of infinite cycle!
    How can I do to continue the execution normally?!
    Thanx...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your problem is you're calling 'fclose' on a null pointer. Bad.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    FILE * Tmp;
    FILE * Tmp2;
    if ((Tmp = fopen("foo.txt", "r+")) == 0)
    {
    	/* file doesn't exist, open and write to it */
    	Tmp2 = fopen("foo.txt" "a");
    	/* write to file here */
    }
    else
    {
    	/* file exists, uh oh */
    }
    fclose(Tmp);
    fclose(Tmp2);
    return 0;
    > it tries to open a file which doesn't exist (I want to update it, so I used "r+");
    How do you "update" a file that doesn't exist?

    I think you could find this information in any decent tutorial by searching google.
    Last edited by Shadow; 04-29-2002 at 02:22 PM.
    The world is waiting. I must leave you now.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Shadow

    > it tries to open a file which doesn't exist (I want to update it, so I used "r+");
    How do you "update" a file that doesn't exist?

    I think you could find this information in any decent tutorial by searching google.
    Well he could just use "w+" which will create the file if it doesn't exist, if I remember correctly.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Yes, so will append. They both create something if it doesn't exist.

    Update = continue, add on
    How do you update something that doesn't exist?
    The world is waiting. I must leave you now.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Shadow
    Yes, so will append. They both create something if it doesn't exist.

    Update = continue, add on
    How do you update something that doesn't exist?
    Well the term 'update' is not really accurate, but I can see the usefulness:

    If it doesn't exist, create the file, if it does, open it for reading and updating.

    Basicly you don't have to have two seperate functions or implementations to handle this.

    Anyway, his code doesn't really need to have anything to do with signal handling. His code is incorrect because, and this is likely why it crashes, he closes a non-existing file pointer.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Handling critical errors
    By Memloop in forum C Programming
    Replies: 14
    Last Post: 03-26-2009, 06:14 AM
  3. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 04:47 AM
  4. need help on error handling.
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 11:55 AM
  5. Signal Handling - Are they part of ANSI C?
    By Stanley S in forum C Programming
    Replies: 3
    Last Post: 12-21-2005, 07:49 AM