|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
checking files
what is the easiest way to check if a file already exists?
|
|
#2
|
|||
|
|||
|
I am fairly new to C/C++, but one alternative would be to check if you can open the file or not.
Sample code: int checkFile(char filename[]) { FILE* fp; // file pointer int retval; // return value fp=fopen(filname, "r"); // open file for reading if (fp) // check if file was opened successfully or not retval=1; else retval=0; return retval; // return 1 or 0 } Basically, if the file already exists, then you will be able to open it. If it doesn't exist, then opening will "fail". Hope this helps Cools Last edited by coolman0stress : April 20th, 2003 at 10:23 PM. |
|
#3
|
|||
|
|||
|
i don't know how to use that library, is there any easy way w/ fstream?
|
|
#4
|
||||
|
||||
|
You could always try opening the file for reading and call the fail() method to check if it failed or not.
Code:
ifstream file;
file.open("filename.txt");
if (file.fail()) {
cerr << "file does not exist";
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > checking files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|