Check if a file exists

I have a bit of prototype code here:
1
2
3
4
5
bool CheckExistence(char* filename)
{
    // I want the program to check if a file with the name filename exists in the 
    // directory I specify.
}

I am working on this and the save and load functions, but I don't know how to check if the file exists in the directory.

Alternately (and, honestly, preferably), I would like to make the function display all the files in the directory with a certain extension so the user can pick which file to save/load.

Could someone explain this to me?
Cancel the first question, I looked it up on msdn.com.

I would still like to know how to build a list of the files with a certain extension (like .fil) that are in the directory.

1
2
3
4
5
#include<brain>
#define Poster_Needs_Help true
if(Poster_Needs_Help)
   cout<<"Help me please!"<<endl;
return fail;
closed account (DSLq5Di1)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428
Alternatively you can use boost filesystem module ( http://www.boost.org/doc/libs/1_51_0/libs/filesystem/doc/index.htm) - it's very convenient. Take a look at directory_iterator for information about iterating over the directory contents.

Also it looks like you're trying to mimic this function:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646928(v=vs.85).aspx
This function opens a "save file" dialog (you can provide your own filter for the files, e.g. *.fil), asks for confirmation upon overwrite - so the usual dialog you see when trying to save file in just about any software.

It depends on what kind of application do you develop. Perhaps you use Qt and then you should use QFileDialog for that purpose: http://doc.qt.nokia.com/4.7-snapshot/qfiledialog.html . If you make a WinAPI software - use the file dialog provided by WinAPI. If you work in Linux - then, well, certainly this functionality is already implemented - just search for it.
Last edited on
Does the windows option work for apps without a WinMain() function?
To be honest, I don't remember - it's been a while since I used WinAPI directly. But as far as I remember MessageBox did work, so I assume this one will too - your program doesn't need to process any windows messages itself. Just try it out :)
As long as you are passing full path

1
2
3
4
5
6
#include <fstream>
bool CheckExistence(char* filename)
{
  ifstream test(filename);
  return test.good();
}
Topic archived. No new replies allowed.