file operation woes

Okay, I am getting some weird behavior with the following code. I am using cygwin, it is a POSIX/Unix emulator for windows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void Socket::registerPlayer(std::string args)
{
	
	fstream pfile;
	std::stringstream fN;

	fN << "../accounts/" << getWord(1,args) << ".pfile";

	ifstream exists(fN.str().c_str());

	if(exists){
		sendCommand("MessageBox", "Error Username already exists, please choose a different one.");
		exists.close();
		return;
	}

	pfile.open(fN.str().c_str(),fstream::trunc | fstream::out);

	pfile << getWord(1, args) << endl;  //username
	pfile << getWord(2, args) << endl;  //display name
	pfile << getWord(3, args) << endl;  //password
	pfile << getWord(4, args) << endl;  //first name
	pfile << getWord(5, args) << endl;  //last name
	pfile << getWord(6, args) << endl;  //email address

	pfile.close();

}


Basically I am attempting to open the player file to see if it exists first, if it does then it will send a command to the client telling them the username is taken, close the file and then return. Here is where it gets weird. First off, even if the file doesn't exist then it will create it, which is not what I want and not what I expected to happen(according to an example I found on this forum). Okay, so it creates the file when it opens it, thus the if check if(exists) is true and it sends the command of the msgbox, closes the file... but does not return. It continues on after the if statement and proceeds to open/create the file and fill it with the player's information... which it should never get to.

So.. what exactly is going on here? And how can I check if a file exists in C++(i.e. not using FILE *file). Any help or insight would be greatly appreciated.
Last edited on
Or std::ios::_Nocreate (I'm not sure if this is the correct. This reference has a suspicious lack of this flag) to the second constructor parameter for 'exists'.

("Or" is a verb.)
The ISO C++ standard does not define any such flag.
The file is being created because fstream is an iostream.

To simply check whether the file exists, use an ifstream.
1
2
3
4
5
6
7
bool file_exists( const string& filename )
  {
  ifstream file( filename.c_str() );
  bool result = file.good();
  file.close();
  return result;
  }

If you are using an older C++ compiler (usually indicated by having to #include <iostream.h>) or Microsoft's VC++, you'll have to use the deprecated ios::nocreate flag. To work with both standards-compliant compilers and VC++, change line 3 to read:
1
2
3
4
5
6
  ifstream file(
    filename.c_str()
    #ifdef _MSC_VER
    , ios::in | ios::nocreate
    #endif
    );

You can identify other compilers using information found here
http://predef.sourceforge.net/

Alas.
Visual C++ recognizes _Nocreate but unfortunately the compiler that I am using to compile the code with does not recognize it and says that it is not a member of std::ios.. :\
Duoas, I just read your reply after I made my previous post. Thank you very much for the information, I haven't tried it yet but it seems like it will work great. Thanks again, and thank you helois as well.
Topic archived. No new replies allowed.