Thread: Help needed with program crash!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    Help needed with program crash!

    Hey all.

    I am trying to make the minesweeper game, but I have one big problem that prevents me of continuing on my nearly finished project.

    I cannot make the game run in turns. Everything runs exactly as I want it to the very first round, but after I input the coordinates of which tile to open in the second round, it crashes. It gives me the following error:

    Code:
    Debug Assertion Failed!
    
    Program: {path}\minesweeper.exe
    File: fprintf.c
    Line: 55
    
    Expression: (str != NULL)
    I click on "Retry" to Debug, and get another window:

    Code:
    minesweeper.exe has triggered a breakpoint
    It then shows me the line it supposedly breaks. The break point vary sometimes depending on my input, but usually it gives me the line I have marked in red.

    You are reminded that this does NOT happen the first time the code is run, but only on the 2nd round.

    Please help!

    Code:
    // SAVING DISPLAY DATA TO FILE
    // TRANSFERRING DATA FROM MinesFile TO DisplayFile.
    
    	MinesFile = fopen("MinesFile.txt","r");
    	DisplayFile = fopen("DisplayFile.txt","w+");
    	for (i=0;i<dimX;i++)
    	{
    		for (j=0;j<dimY;j++)
    		{
    
    			ch = fgetc(MinesFile);
    			if ((ch == '0') || (ch == '1'))
    			{
    				fprintf(DisplayFile,"#");
    			}
    			else if (ch == '2')
    			{
    				count = check4Neighbours(dimY,dimX,i,j);
    				if (count == 0)
    				{
    					fprintf(DisplayFile,".");
    				}
    				else
    				{
    					fprintf(DisplayFile,"%d",count);
    				}
    			}
    		}
    	}
    	fclose(DisplayFile);
    	fclose(MinesFile);
    Last edited by dcdude; 04-27-2011 at 09:20 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Check fopen() return value.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Hmm, ok I did. I got NULL on DisplayFile... how could that be? It works fine the first time!

    Thanks for your reply Naung..

    UPDATE:

    Fixed the problem. I forgot an fclose in a previous function.
    Last edited by dcdude; 04-27-2011 at 09:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this program crash when I do this?
    By Witch in forum C Programming
    Replies: 4
    Last Post: 03-18-2010, 07:10 PM
  2. my program crash?
    By vrkiller in forum C++ Programming
    Replies: 10
    Last Post: 03-04-2009, 03:03 PM
  3. Why Does My Program Crash?
    By Grantyt3 in forum C++ Programming
    Replies: 20
    Last Post: 09-29-2005, 05:34 PM
  4. Why does the program crash?
    By SkyRaign in forum C++ Programming
    Replies: 8
    Last Post: 09-25-2005, 10:06 AM
  5. Program crash ?!
    By tilex in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2004, 08:47 PM