Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

C++ can you check if a file exists?

Original Message
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

Report Offensive Message For Removal


Response Number 1
Name: egkenny
Date: October 6, 2004 at 18:03:15 Pacific
Subject: C++ can you check if a file exists?
Reply: (edit)
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;
}



Report Offensive Follow Up For Removal

Response Number 2
Name: tImmaY
Date: October 6, 2004 at 21:49:18 Pacific
Subject: C++ can you check if a file exists?
Reply: (edit)
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

Report Offensive Follow Up For Removal

Response Number 3
Name: tImmaY
Date: October 10, 2004 at 00:49:38 Pacific
Subject: C++ can you check if a file exists?
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 4
Name: crondeemon
Date: October 22, 2004 at 08:08:11 Pacific
Subject: C++ can you check if a file exists?
Reply: (edit)
bool flag = false;
fstream fin;
fin.open("data.txt",ios::in);
if( fin.is_open() )
{
cout<<"file exists"<<endl;
flag=true;
}
fin.close();



Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C++ can you check if a file exists?

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




CPU and Graphics Upgrade Questions

VIRUS ALERT in Taskbar, HELP!

DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12


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

All content ©1996-2007 Computing.Net, LLC