Computing.Net > Forums > Programming > C++ can you check if a file exists?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ can you check if a file exists?

Reply to Message Icon

Name: tImmaY
Date: October 6, 2004 at 11:47:19 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: egkenny
Date: October 6, 2004 at 18:03:15 Pacific
Reply:

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;

myFileName = "mydata.txt";
inp.open(myFileName.c_str(), ifstream::in);
inp.close();
if(inp.fail())
{
inp.clear(ios::failbit);
cout << "Writing to file..." << myFileName.c_str() << endl;
out.open(myFileName.c_str(), ofstream::out);
out << "Hello World" << endl;
out.close();
}
else
{
cout << "Error...file """ << myFileName.c_str() << """ exists" << endl;
}
return 0;
}



0

Response Number 2
Name: tImmaY
Date: October 6, 2004 at 21:49:18 Pacific
Reply:

hmm.. i'll have to try to integrate that into my code tomorrow and repost where i get from there and maybe my code. but thanks


0

Response Number 3
Name: tImmaY
Date: October 10, 2004 at 00:49:38 Pacific
Reply:

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:

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
string message;
cout << "\nEnter message \n";
getline(cin,message);
ofstream writeIt("writtenString.shibby", ios::app);
writeIt << message << endl;
writeIt.close();
system("notepad.exe writtenString.shibby");
system ("PAUSE");
return 0;
}

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();
}
}

so.. help? please? lol


0

Response Number 4
Name: crondeemon
Date: October 22, 2004 at 08:08:11 Pacific
Reply:

bool flag = false;
fstream fin;
fin.open("data.txt",ios::in);
if( fin.is_open() )
{
cout<<"file exists"<<endl;
flag=true;
}
fin.close();



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ can you check if a file exists?

Can't check if input file was chose www.computing.net/answers/programming/cant-check-if-input-file-was-chose/17320.html

checking a file exist or not www.computing.net/answers/programming/checking-a-file-exist-or-not-/2599.html

Check for file existance on server www.computing.net/answers/programming/check-for-file-existance-on-server/16291.html