Name: tImmaY Date: October 6, 2004 at 11:47:19 Pacific Subject: C++ can you check if a file exists? OS: Windows XP Pro CPU/Ram: AMD Athlon XP 2400+ / 512
Comment:
hey, i was wondering if you could check if a file exists in C++. like maybe using parts of fstream? otherwise is there another way that you can check if a file exists? because in my program i want to have it rename a file if it does exist and if it doesn't, then i just want the program to keep going.. it seems to be getting hung up b/c the file doesn't exist. thanks in advance ::tim
The trick is to use the ios flags. Make sure you reset any error flags after you test them. The following program will only write to a file if it does not exist.
#include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ifstream inp; ofstream out; string myFileName;
ok, well i added that part of the code into mine and if the file exists, it sets a variable to 1, if not it sets it to 0. the compiler has no problem with that, but i want to accept strings of text for group names and items on the list but for some reason, this code is being retarded. maybe someone can help me with it.. ok, this code works:
that will take a string of text, write it to a file and then open the file with the string that you've typed in notepad. but for some reason when i try to integrate that into my code, i get an error from the compiler.. [linker error] undefined reference to group(). but heres my code:
void group(int argc, char *argv[]) { cout << "What do you want the title of this group to be?" << endl; cout << "(Note: Group will be added to end of list.)\n::"; getline(cin,newgroup); cout << "You typed "<< newgroup.c_str() << " :: Is this correct?" << endl; cout << "Enter 1 for yes or 2 for no.\n::"; cin >> choice; if (choice == 1) { WriteToFile(); } else if (choice == 2) { group(); } else { cout << "Error. Improper choice. Returning to main." << endl; system("Pause"); main1(); } }
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE