how do you check if a file exists before you attempt to open it or write to it?
how do you check if a file exists before you attempt to open it or write to it?
Usually you try to open it and if it fails to open then you handle that error.
You can attempt to open it for reading. If that succeeds then the file exists. If the file doesn't exist then it will fail to open for reading.
There could always be some other error causing an existing file not to open, but that will be fairly rare. Depending on what platform you're on there are other more specific ways to tell that might be more accurate.
Isn't there a way to browse the existing folder? This should be portable through unix solaris linux windows and all the major ones.
There is no standard C++ solution. I would try boost's filesystem library. There may also be Posix functions that will work across the platforms you need.
thanks, i'll have to get my co-worker to make the funciton give a return value on his dataset loading function then.
If it's only those OS's that you're concerned with, then they all should support the stat() function which gets info about a file. Other more exotic OS's may not support stat().
But it would still be a good idea to abstract out the function you're trying to do and call whatever OS specific function is available on your platform.
thank you
what about this : http://www.cplusplus.com/reference/i...ebuf/open.html
What are its limitations to this for checking if a file exists in a folder or not?
This is the same as my original suggestion (although I'm not sure why you need to use the filebuf, just use the ifstream). If the file fails to open for reading, you won't be able to tell if it doesn't exist or if there was some other error. Otherwise it's fine.
What do you mean "first bit"?
If the file does not exist, the function returns null. If the file exists but cannot be opened for reading (perhaps because of certain permissions), the function returns null. How can you tell the difference?
As I said, you might not care about the difference, in which case simply using the ifstream will be fine, but it isn't 100% accurate.
Daved is asking you what you mean by "exists". Do you mean, that you can access it? Then just trying to open it will answer your question. Do you mean, whether the file is in the filesystem somewhere, even if it is locked or protected? You will have to use nonstandard system calls to answer that.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
I don't know any good resources off the top of my head. What are you confused about?
It's pretty simple. If you try to open a file for reading and it fails, most of the time that's because it doesn't exist. Occasionally it is because of some other error (like a lack of read permissions).
If you try to open the file with an fstream (or ifstream or filebuf) then the only information you get is that the file failed to open. You don't know if it is because it does not exist or if it is because the user does not have permission to read that file.
So the question is, for your program, is it good enough to just know that it failed without knowing why? If you are writing files in the same directory in your application, then it will be very rare that you won't be allowed to read one. So maybe it's ok.
If it's not ok, then use a third party library like boost filesystem instead of using the fstream open trick.