What is wrong with this file i/o code?

Started by
1 comment, last by CTRL_ALT_DELETE 22 years, 6 months ago
include "levels.h" extern HWND window_handle; void levels::eatline() { //eat the spaces at the end of the line char ch; ch = m_fin.get(); while(ch != ''\n'') { ch = m_fin.get(); } } levels::levels(char* str) { bOK = TRUE; //open the level file m_fin.open(str, ios::in); //if the file does not open, bail out if(!m_fin.is_open()) { bOK = FALSE; DestroyWindow(window_handle); return; } for(int i = 0; i < 4; i++) { for(int n = 0; n < 10; n++) level[n] = m_fin.get(); eatline(); } m_fin.close(); } This code feeds text data from a file into a 2d array that I use in my game to place the enemies in my Galaxian code ( a sorrt of primative level editor). m_fin is created earlier in the code, and is associated with a file named levelx.txt, where x is the number of the level. The problem is, if the game tries to load a level file that I have not yet created, such as level6.txt, it freezes. Shouldn''t this code bail it out if the file does not exist? if(!m_fin.is_open()) { bOK = FALSE; DestroyWindow(window_handle); return; } Thanks, Austin
Advertisement
Try
m_fin.open(str, ios::in|ios::nocreate); 

instead.
fin.open(filename, ios::in|ios::nocreate);

this will cause the fin.is_open() function to return false if the filename does not exist, otherwise, just using ios::in assumes that if the filename does not exist then you want to create a new file.

This topic is closed to new replies.

Advertisement