ofstream and rdstate()
From: Edo (edwardoJE_at_aking.com)
Date: 06/29/04
- Previous message: Poison64: "int -> hex string"
- Next in thread: Francis Glassborow: "Re: ofstream and rdstate()"
- Reply: Francis Glassborow: "Re: ofstream and rdstate()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 29 Jun 2004 03:58:40 -0700
Hello
I am trying to check for the existence of a file name in a given
directory before I attempt to create this file, I don't want to replace
the file if it is already there.
I don't know HowTo, returning the ios::noreplace rdstate() is not the
same as not attempting the operation if the file exists from the start.
Thanks
// generate a Studnet file with random grades for
// accelarated C++ exercise 5-2
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <fstream>
using std::ofstream;
#include <string>
using std::string;
int main()
{
// get number of lines from user
cout << "Please indicate number of lines you want in a "
"student file: ";
int num;
cin >> num;
// get a name for the file
cout << "Enter a name for this file: ";
string s;
cin >> s;
// if file exists, ask for a different name, well, thats all I know
ofstream oFile(s+".txt", ios::noreplace);
if (oFile.rdstate() == ios::failbit){
cout << "File already exists\n"
"Please choose another name: ";
cin >> s;
}
cout << "\n\n----- CREATED -----\n"
"C:\\Some_Path" << s << endl
<< "Number of lines: " << num << endl;
system("PAUSE");
return 0;
}
- Previous message: Poison64: "int -> hex string"
- Next in thread: Francis Glassborow: "Re: ofstream and rdstate()"
- Reply: Francis Glassborow: "Re: ofstream and rdstate()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|