Writing to file isnt working

This is a discussion on Writing to file isnt working within the C Programming forums, part of the General Programming Boards category; Code: int main(void) { char game_board[NUM_ROWS][NUM_COLS] = {{'~', '~'}, {'~'}}; char game_board2[NUM_ROWS][NUM_COLS] = {{'~', '~'}, {'~'}}; char game_board3[NUM_ROWS][NUM_COLS] = {{'~', ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    33

    Writing to file isnt working

    Code:
    int main(void)
    {
    	char game_board[NUM_ROWS][NUM_COLS] = {{'~', '~'}, {'~'}};
    	char game_board2[NUM_ROWS][NUM_COLS] = {{'~', '~'}, {'~'}};
    	char game_board3[NUM_ROWS][NUM_COLS] = {{'~', '~'}, {'~'}};
    	char game_board4[NUM_ROWS][NUM_COLS] = {{'~', '~'}, {'~'}};
    	char ship, target;
    	int direction = 0,compc = 0, compr = 0,count = 1,count_miss = 0, count_hit = 0, end = 0, row = 10, col = 10, status = 0,rows = 10, cols = 10, option = 0,target_r = 0, target_c = 0, place = 0, int_check_shot = 0;
    	
    	//srand( (unsigned)time( NULL ) );
    	srand (time(NULL));
    
    	init_game_board (game_board, 10, 10);
    	init_game_board (game_board2, 10, 10);
    	init_game_board (game_board3, 10, 10);
    	init_game_board (game_board4, 10, 10);
    
    	FILE *outfile = NULL;
    
    	outfile = fopen ("Battleship.log", "w");
    	
    
    	do
    	{
    
    		welcome_screen();
    	
            system("pause");
            system("cls");
    
    		menu();
    
    		
    		
    		if (menu == 1)
    		{
    			system("pause");
    			system("cls");
    
    			manually_place_ships(game_board, 'c');
    			print_game_board(game_board, 10, 10);
    
    			system("pause");
    			system("cls");
    		
    			manually_place_ships(game_board, 'b');
    			print_game_board(game_board, 10, 10);
    
    			system("pause");
    			system("cls");
    
    			manually_place_ships(game_board, 'r');
    			print_game_board(game_board, 10, 10);
    
    			system("pause");
    			system("cls");
    	        
    			manually_place_ships(game_board, 's');
    			print_game_board(game_board, 10, 10);
    
    			system("pause");
    			system("cls");
    
    			manually_place_ships(game_board, 'd');
    			print_game_board(game_board, 10, 10);
    
    			system("pause");
    			system("cls");
    	
    		}
    		else
    		{
    			while(place_ship(game_board, 5, col, row, direction, 'c') == 0);
    			while(place_ship(game_board, 4, col, row, direction, 'b') == 0);
    			while(place_ship(game_board, 3, col, row, direction, 'r') == 0);
    			while(place_ship(game_board, 3, col, row, direction, 's') == 0);
    			while(place_ship(game_board, 2, col, row, direction, 'd') == 0);
    		}
    		
    		while(place_ship(game_board2, 5, col, row, direction, 'c') == 0);
    		while(place_ship(game_board2, 4, col, row, direction, 'b') == 0);
    		while(place_ship(game_board2, 3, col, row, direction, 'r') == 0);
    		while(place_ship(game_board2, 3, col, row, direction, 's') == 0);
    		while(place_ship(game_board2, 2, col, row, direction, 'd') == 0);
    		
    		do
    		{
    			system ("cls");
    			
    		
    			printf("*******************************************************\n");
    			printf("**                     ROUND %d                      **\n", count);
    			printf("*******************************************************\n");
    			printf(" players turn\n"); 
    			
    			print_game_board(game_board, 10, 10);
    			printf(" Players Target Board\n"); 
    			print_game_board(game_board3, 10, 10);
    			get_target (&target_r, &target_c);
    			int_check_shot = check_shot(game_board2, target_r, target_c);
    			if (int_check_shot == 0)
    			{
    				count_miss++;
    			}
    			else
    			{
    				count_hit++;
    			}
    			update_board(game_board3, int_check_shot, target_r, target_c);
    
    			system ("pause");
    			system ("cls");
    
    			print_game_board(game_board3, 10, 10);
    			end = is_winner(game_board);
    			end = is_winner(game_board2);
    			
    			system ("pause");
    			system ("cls");
    			
    			printf("*******************************************************\n");
    			printf("**                     ROUND %d                      **\n", count);
    			printf("*******************************************************\n");
    			printf(" computers turn\n"); 
    
    			print_game_board(game_board4, 10, 10);
    			// right a function that creats two numbers 0 9  // if * M
    			compr = rand () % 9;
    			compc = rand () % 9;
    			// printf what cordintest it shot
    			printf("Computer Target Coordinate (%d,%d)\n", compr, compc); 
    			int_check_shot = check_shot(game_board, compr, compc);
    			update_board(game_board4, int_check_shot, compr, compc);
    		
    			system ("pause");
    			system ("cls");
    
    			print_game_board(game_board4, 10, 10);
    			end = is_winner(game_board);
    			end = is_winner(game_board2);
    			
    			system ("pause");
    
    			count++;
    		}while (end == 0);
    		
    
    	}while (status == 1);
    	{
    		fprintf ("Battleship.log", "%d\n%d\n%d\n", count, count_hit, count_miss);
    	}
    
    	fclose ("Battleship.log");
    
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Really? You just dump a hundred lines of code with no actual question or explanation or anything? Nice.


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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You have that file open the entire time but never write to it until the end.
    I do hope you didn't expect it to record the whole game...

    Anyway, your bit at the end should be...
    Code:
    // other code
    	}while (status == 1);
    
    outfile = fopen ("Battleship.log", "w");
    if ( ! outfile )
      { printf("unable to open Battleship.log file");
         exit(1); } 
    
    fprintf (outfile, "%d\n%d\n%d\n", count, count_hit, count_miss);
    fclose (outfile)
    return 0;
    }
    Your compiler should have been screaming bloody murder about using a string in place of a file pointer... and since most compiler errors include line numbers, it should have taken you directly to the problem.

    Strategically it is a bad idea to have a file open any longer than it needs to be.
    Also you should always check the return value of any I/O operation... they DO fail sometimes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 11:05 AM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  4. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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