• Advertisement

Archived

This topic is now archived and is closed to further replies.

What is wrong with this file i/o code?

This topic is 5993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

  • Advertisement