Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: tImmaY
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;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;
}

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

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

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

![]() |
![]() |
![]() |

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