Thread: fprintf

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    fprintf

    i've found a problem i really have no idea how to solve. in a Win32 pure API program, i declare a global FILE pointer (im not sure if global is the right term, but the code below will show what i mean). in WM_CREATE, i fopen() a file. in WM_COMMAND, when i try to use fprintf, it exits the program and gives me a second chance assertion failure, in fprintf.c line 56. the way it exits the program is also interesting. the window is destroyed, and a messagebox pops up saying, "User breakpoint called from code at 0xbff768a4". that makes me think this is more than just a simple error, because i dont have any breakpoints whatsoever, but the fprintf() call is causing one. and by the way, that address, "0xbff768a4", is always the same.

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    FILE *fp;
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
              ...
              case WM_CREATE:
                     if ( (fp=fopen("file.dat","wb"))==NULL)
                              exit(1);
            
              ...
            case WM_COMMAND:
                   fprintf(fp,"......"); //this line causes the error
    
            case WM_CLOSE:
                 fprintf(fp,"Shutting down"); //but this one works fine
                 fclose(fp);
    
    }
    i orginally thought maybe fopen() stored some important information on the local stack, which then got popped when the Window Procedure function returned, but if that was the case, the fprintf() in WM_CLOSE wouldn't work.

    no, i just cant see any reason whatsoever why this is happening. someone out there must know what to do.
    Last edited by bennyandthejets; 11-13-2002 at 04:29 PM.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >>case WM_COMMAND:
    >>fprintf(fp,"......"); //this line causes the error


    Change to:

    case WM_COMMAND:
    if(fp) fprintf(fp,"......"); //this line causes the error
    else MessageBox(NULL, "The file was not ready!", "How odd!", MB_OK);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    yeah it appears that fp becomes NULL at that stage. but why then does it work during WM_CLOSE? i really want to have the file open the whole time, for debugging purposes.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try making the FILE* static...also, what are you doing to invoke the WM_COMMAND? I mean, does it crash before you do anything(during startup)?


    /*edit*/

    Also, open the file when you declare the FILE* and see what happens...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: fprintf

    Originally posted by bennyandthejets
    Code:
    if ( (fp=fopen("file.dat","wb"))==NULL)
    Why do you open the file in binary mode if you use the fprintf function?

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    sebastiani, what does static do, and how do i declare something static? and i have no idea what invokes the WM_COMMAND, it just happens before there is any user interaction. ill check the LOWORD(wParam) and see what the ID is.
    /.... some time passes.../
    well, the WM_COMMAND is being invoked by the edit box in the window, which is the only control there. it must be sent when the edit control is initialized.

    monster, it makes no difference to take it off binary mode, it still appears that the file pointer is NULL during WM_COMMAND.

    /.... some time passes.../
    YES! SUCCESS! sebastiani (thank you thank you thank you), i put the fopen() where i declare the gobal FILE pointer, and it works! the problem must have been that when i used fopen(), it stored vital data in a local memory area which is unavailable during WM_COMMAND and similar messages (remember, everything worked during WM_CLOSE). although the problem is solved, it would still be nice to know why this occurs. any ideas?
    Last edited by bennyandthejets; 11-14-2002 at 05:03 PM.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Strange as it may sound...try this:

    Code:
    FILE *fp;
    bool unexpected = true;
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
              ...
              case WM_CREATE:
                     if ( (fp=fopen("file.dat","wb"))==NULL)
                              exit(1);
                    unexpected = false;
            
              ...
            case WM_COMMAND:
        if(unexpected) {
         MessageBox(NULL, "A WM_COMMAND was sent before WM_CREATE!", "Aha!", MB_OK);
        } else {
        fprintf(fp,"......"); 
        }
            case WM_CLOSE:
                 fprintf(fp,"Shutting down"); //but this one works fine
                 fclose(fp);
    
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    that solved part of the problem. some WM_COMMAND messages come before WM_CREATE, so the file is not open yet. but still, after it is opened it still thinks its not. i might just have to settle for opening the file globally, but id still like to know whats going on here!
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    is that MessageBox, what does MB_OK mean anyways? i understand the first message is the body and the second is the titlebar, but whats MB_PK mean? what does it do?
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It means "this is a message box with an 'OK' button"...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    k that makes sense
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fprintf()
    By Viper187 in forum C Programming
    Replies: 2
    Last Post: 06-22-2008, 04:52 AM
  2. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  3. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  4. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 10:55 AM
  5. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 08:51 AM